I tried a few changes like SyncLock and CanWrite but it is still having the same issue:
vb Code:
  1. ''' <summary>
  2.     ''' Sends binary data.
  3.     ''' </summary>
  4.     Public Sub sendData(ByVal data() As Byte, Optional ByVal curStream As NetworkStream = Nothing)
  5.  
  6.         Try
  7.             SyncLock curStream
  8.                 Dim endbytes() As Byte = System.Text.Encoding.ASCII.GetBytes("{682427e7-6a56-4513-87f1-2dec81ddae2d}")
  9.  
  10.                 Dim lenBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(data.Length)
  11.  
  12.                 If curStream.CanWrite Then
  13.                     curStream.Write(lenBytes, 0, lenBytes.Length)  '<----------- It is getting stuck here sometimes
  14.                 End If
  15.                 If curStream.CanWrite Then
  16.                     curStream.Write(endbytes, 0, endbytes.Length)  '<----------- It is getting stuck here sometimes
  17.                 End If
  18.                 If curStream.CanWrite Then
  19.                     curStream.Write(data, 0, data.Length)
  20.                 End If
  21.                 curStream.Flush()
  22.             End SyncLock
  23.         Catch
  24.  
  25.         End Try
  26.  
  27.         'If curStream IsNot Nothing Then
  28.         '    curStream.Write(data, 0, data.Length)
  29.         'End If
  30.  
  31.     End Sub