Ok, still, all of those variables are declared in the UI thread. That means that any routine that is called by DoWork cannot use them. Also, you need to remove the MsgBox calls from DoWork. MessageBox should be called from ProgressChanged.

These cannot be accessed by any routine that is called by your backgroundworker:

vb.net Code:
  1. Dim clNo As String
  2. Dim serverSocket As New TcpListener(8888)
  3. Dim clientSocket As TcpClient
  4. Dim counter As Integer
  5.  
  6. Dim requestCount As Integer
  7. Dim bytesFrom(10024) As Byte
  8. Dim dataFromClient As String
  9. Dim sendBytes As [Byte]()
  10. Dim serverResponse As String
  11. Dim rCount As String
  12.  
  13. Dim RtnString As String = ""
  14.  
  15. Dim D(20) As String