Sub Waitfordata_Tick
Dim tbuf(8192) As byte
If Client.DataAvailable = True Then
count = Stream.ReadBytes(tbuf(),8192)
ArrayCopy(tbuf(),0,count,Q(),endpoint)
endpoint = endpoint + count
If readflag = False Then ' the size was not read yet, read it
ArrayCopy(Q(),0,4,tb(),0)
MsgSize = bc.Int32FromBytes(tb(),0)
readflag = True ' size was read
End If
If endpoint >= MsgSize+4 AND readflag Then ' the message is all in the Q, get it
Dim buffer(MsgSize) As byte
ArrayCopy(Q(),4,MsgSize,buffer(),0) ' copy msg to buffer
ArrayCopy(Q(),4+msgsize,endpoint - 4 - MsgSize,Q(),0)
endpoint = endpoint - MsgSize - 4
readflag = False ' next data in Q is a size
NewMsg ' using buffer with the exact MsgSize size
End If
End If
End Sub