Their is only one easy way that you can make it go faster and that is to use a background worker. The code itself has nothing to do to how fast or how slow your application works, it is where you place the code. I suspect you have a timer or call the class (I don't know because i am not a mind reader). This is the way I would attempt it.
1. First add 2 background workers to your project.
2. In the first background worker put your first code in.
Public Class HANDLESERVER
Dim CLIENTSOCKET As TcpClient
Dim CLNO As String
Public Sub STARTCLIENT(ByVal INCLIENTSOCKET As TcpClient, ByVal CLINENO As String)
Me.CLIENTSOCKET = INCLIENTSOCKET
Me.CLNO = CLINENO
Dim CTTHREAD As Threading.Thread = New Thread(AddressOf RESPONSEREQUEST)
CTTHREAD.Start()
End Sub
Private Sub RESPONSEREQUEST()
Dim REQUESTCOUNT As Integer
Dim DATAFROMCLIENT As String
REQUESTCOUNT = 0
While (True)
Try
REQUESTCOUNT = REQUESTCOUNT + 1
Dim NETWORKSTREAM As NetworkStream = CLIENTSOCKET.GetStream()
Dim BYTESFROM(10024) As Byte
NETWORKSTREAM.Read(BYTESFROM, 0, CInt(CLIENTSOCKET.ReceiveBufferSize))
For I As Integer = 0 To 10024
If BYTESFROM(I) = 0 Then
NEXTPOS = I
Exit For
End If
Next
DATAFROMCLIENT = System.Text.Encoding.ASCII.GetString(BYTESFROM)
DATAFROMCLIENT = DATAFROMCLIENT.Substring(0, NEXTPOS)
DataFrimAts = DATAFROMCLIENT
If DataFrimAts <> "" Then
Dim D As New DLG_tRIAL(AddressOf _TIMER_DATA)
fmData.rchWithoutParse.Invoke(D)
End If
Array.Clear(BYTESFROM, 0, 10024)
NEXTPOS = 0
Catch ex As Exception
End Try
End While
End Sub
End Class
3. In second background worker put your second code in.
Sub Socket_listner()
Dim serversocket4 As New TcpListener(ATCPORT)
Dim clientsocket4 As TcpClient
Dim counter As Integer
serversocket4.Start()
counter = 0
While (True)
counter += 1
clientsocket4 = serversocket4.AcceptTcpClient
Dim client2 As New HANDLESERVER
client2.STARTCLIENT(clientsocket4, "1")
End While
clientsocket4.Close()
serversocket4.Stop()
End Sub
4. Now you need to stop your program from checking for illegalcrossthreadcalls. Put this code in.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
5. know when you want to run your code you simply run the background worker.
BackgroundWorker1.RunWorkerAsync()
BackgroundWorker2.RunWorkerAsync()
5. DONE
This should do the trick :) Hope it helps.