title: Using ThreadPool to connect to multiple TCP servers simultaneously
body: <pre><code>Imports System.IO
  Imports System.Threading
  Imports System.Net.Sockets
  Imports System.Text
  
  Module Module1
  &lt;MTAThread()&gt;
  Sub Main()
      Dim maxthreads As Integer = 10
      ThreadPool.SetMaxThreads(maxthreads, maxthreads)
      Dim serverlist() As String = File.ReadAllLines("final.txt")
  
      Dim servernum As New CountdownEvent(serverlist.GetUpperBound(0) + 1)
      For Each a In serverlist
          Dim args(1) As Object
          args(0) = a
          args(1) = servernum
          ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ping), args)
      Next
      servernum.Wait()
  
  
      Console.ReadLine()
  End Sub
  
  Public Sub ping(ByVal server() As Object)
  
      Dim serverinfo() As String = Split(server(0), ",")
      Dim socketclient As New TcpClient
      Console.WriteLine(serverinfo(0))
  
      Try
          socketclient.Connect(serverinfo(0), serverinfo(1))
      Catch ex As Exception
          If socketclient.Connected = False Then
              Throw New Exception("Server Offline")
          Else
              Throw New Exception("Unknown Error Occured")
          End If
      End Try
  
         Dim stream As NetworkStream = socketclient.GetStream
          stream.ReadTimeout = 1000
          stream.WriteTimeout = 1000
          Dim sendBytes As [Byte]() = {&amp;HFE}
          stream.Write(sendBytes, 0, sendBytes.Length)
          Dim bytes(421) As Byte
          stream.Read(bytes, 0, CInt(421))
          socketclient.Close()
          Dim trimbytes(bytes.Length - 3) As Byte
          Array.Copy(bytes, 3, trimbytes, 0, bytes.Length - 3)
          Dim returndata As String = Encoding.BigEndianUnicode.GetString(trimbytes)
  
          Dim sb As New System.Text.StringBuilder
          For i As Integer = 1 To 241 Step 2
              If trimbytes(i) &lt;&gt; 0 Then
                  sb.Append(ChrW(BitConverter.ToInt16(trimbytes, i)))
              End If
          Next i
          Dim message() As String = sb.ToString.Split("§")
  
          'Write processed server information to the console
          Console.WriteLine("Received --&gt;" &amp; message(1) &amp; " " &amp; message(2) &amp; " | " &amp; serverinfo(0))
  
          server(1).signal()
  
  
  
  End Sub
  End Module
  </code></pre>
  
  <p>Above is my code, what I basically want to do is to get two (or more) ips from the text file, connect to both of them via TCP on two different threads and get the responses from the servers.</p>
  
  <p>Now, the problem here is that; for whatever reason one of the two servers get a response but the other doesn't. I've made sure both of the servers are online and accepting connections. I've also tried having the same server on both lines of the text file so that the same ip is being used on both threads but this doesn't work either. Even when the two ips are the same, one gets a response but not the other; which must mean I'm doing something horribly wrong.</p>
  
  <p>When I use one server rather than 1+ everything works perfectly and I get the response I'm looking for, anything more than 1 and it looks like the first thread that runs produces the response but not anything that runs later. Any help will be appreciated.</p>
  
  <p>I have also tried using Paarallel.For but ended up running into the same issue.</p>
  
  <pre><code>Parallel.ForEach(serverlist, _
               Sub(currentElement)
                   ping(currentElement)
               End Sub)
  </code></pre>