title: VB.net TCPListner works once, then acts strange
body: <p>I am working on writing a TCP listener to accept printer output for a project.  I have written the following code, and it works great, once. After the first print job is processed, it acts strange.</p>
  
  <p>At runtime the program sits at the line tcpClient = TheTCPListener.AcceptTcpClient() until something connects, then it processes the data being sent.  The first print job works great. If I send 5 more print jobs, they may all go through, or they may sit on the tcpClient = TheTCPListener.AcceptTcpClient() line for a while, then keep processing the code without getting any data, and loop back to that spot again.  After the first time, it and may or may not actually get any data, but will keep looping as long as there is something in the print queue waiting to send.  Eventually, it will process all of the print jobs, but I can't figure out why it works perfectly once (EVERY TIME) then may or may not work correctly the next time data is sent.</p>
  
  <p>Any help or suggestions are VERY appreciated!</p>
  
  <pre><code>    Public Sub EnableListener()
      Dim TheTCPListener As New TcpListener(IPAdr, PortNumber)
      Dim tcpClient As TcpClient = Nothing
      Dim networkStream As NetworkStream = Nothing
      Dim UseThisFileName As String = ""
      Dim bytestoread As Integer = 0
      Dim temploopcount As Integer = 0
  
      'start the TCP listener
      TheTCPListener.Start()
  
      While (True)
         'Reset The Size of the byte chunk
          bytestoread = 0
  
          'Wait for connection
          tcpClient = TheTCPListener.AcceptTcpClient()
  
          'Set the buffer size large enough to handle the whole file
          tcpClient.ReceiveBufferSize = 60000000
  
          'set the filename to write too
          UseThisFileName = getfilenameext()
  
          networkStream = tcpClient.GetStream
  
          'verify that the stream has data to read 
          While networkStream.DataAvailable
  
              networkStream.Read(bytes, 0, 60000000)
              bytestoread = 0
  
          ' Get the size of the bytes with data
              For x = 0 To 59999999
                  If bytes(x) &lt;&gt; Nothing Then
                      bytestoread = bytestoread + 1
  
                  End If
              Next
  
             'calls the sub to append to the file 
             writebtyestofile(PrintFolder + UseThisFileName, bytestoread)
  
             'resets the bytes array to empty 
             For x = 0 To 59999999 bytes(x) = Nothing Next
  
          End While
  
          'Close TcpClient.  Without this, the print job never finishes sending in the print queue
          tcpClient.Close()
      End While 'this loops the listener forever, unless the program is shut down.
  End Sub
  
  </code></pre>