title: How to fix error "Only one usage of each socket address is normally permitted"
body: <p>I have 1 process that acts as a server (TcpListener) and one or more processes that act as clients (TcpClient). The clients are sending Tcp requests to the server every 1-2 seconds to which the server responds with a result message.</p>
  
  <p>In normal conditions the server will use TcpListener.Stop when it closes, but sometimes the server will crash (for whatever reason) and then this is not done. If I then try to restart then I get the following error:</p>
  
  <p>"Only one usage of each socket address (protocol/network address/port) is normally permitted"</p>
  
  <p>I <em>only</em> get this error when there are clients running who keep trying to connect despite the TcpListener is not there anymore (crashed).</p>
  
  <p>I <em>don't</em> get this error when I first manually close/kill all clients before restarting the server/TcpListener.</p>
  
  <p>I'm looking for an automatic recovery procedure that deals with this error/problem. How can this be done?</p>
  
  <p>Below you'll find code examples of the server and the client.</p>
  
  <p>BTW: Although the codes examples are in VB.NET, I can also handle C# code. That's why I tagged this question with both VB and C#.</p>
  
  <p>Code example for server/TcpListener:</p>
  
  <pre><code>Public Shared Sub Thread_TcpListener_GUI()
  
      While True
  
          Dim tcpClient As TcpClient = Nothing
          Dim tcpListener As TcpListener = Nothing
  
          Try
  
              tcpListener = New TcpListener(System.Net.IPAddress.Any, 4960)
              tcpListener.ExclusiveAddressUse = False
              Call tcpListener.Start()
  
              While True
  
                  tcpClient = tcpListener.AcceptTcpClient
  
                  Dim tcpNetworkStream As NetworkStream = tcpClient.GetStream()
  
                  Dim strMessageReceived As String = ""
  
                  'Get received message
                  While tcpNetworkStream.DataAvailable
                      Dim byteReceived(tcpClient.ReceiveBufferSize) As Byte
                      tcpNetworkStream.Read(byteReceived, 0, CInt(tcpClient.ReceiveBufferSize))
                      strMessageReceived &amp;= Encoding.UTF8.GetString(byteReceived).Trim(Convert.ToChar(0))
                  End While
  
                  'Send response message
                  If strMessageReceived &lt;&gt; "" Then
                      Dim strResponse As String = "Received!"
                      Dim byteResponse As Byte() = Encoding.UTF8.GetBytes(strResponse)
                      tcpNetworkStream.Write(byteResponse, 0, byteResponse.Length)
                  End If
  
              End While
  
          Catch ex As Exception
              Call Console.WriteLine(ex.Message)
  
          Finally
  
              Try
                  If tcpClient IsNot Nothing Then
                      If tcpClient.Connected Then
                          tcpClient.Close()
                      End If
                  End If
              Catch ex As Exception
              End Try
  
              Try
                  If tcpListener IsNot Nothing Then
                      tcpListener.Stop()
                      tcpListener = Nothing
                  End If
              Catch ex As Exception
              End Try
  
              Thread.Sleep(1500)
  
          End Try
  
      End While
  
  End Sub
  </code></pre>
  
  <p>Code example client:</p>
  
  <pre><code>Public Shared Sub TcpClient_SendMessage(ByVal strMessage As String)
  
      Dim tcpClient As System.Net.Sockets.TcpClient = Nothing
      Dim strResponse As String = ""
  
      While True
  
          Try
  
              tcpClient = New System.Net.Sockets.TcpClient()
              tcpClient.ExclusiveAddressUse = False
  
              tcpClient.Connect("localhost", 4960)
  
              Dim tcpNetworkStream As NetworkStream = tcpClient.GetStream()
  
              'Send message
              Dim byteMessage As Byte() = Encoding.UTF8.GetBytes(strMessage)
              tcpNetworkStream.Write(byteMessage, 0, byteMessage.Length)
  
              'Get respronse message
              strResponse = ""
              While tcpNetworkStream.DataAvailable
                  Dim byteResponse(tcpClient.ReceiveBufferSize) As Byte
                  tcpNetworkStream.Read(byteResponse, 0, CInt(tcpClient.ReceiveBufferSize))
                  strResponse &amp;= Encoding.UTF8.GetString(byteResponse).Trim(Convert.ToChar(0))
              End While
  
              'Do something with response here ....
  
          Catch ex As Exception
  
              Console.WriteLine(ex.Message)
  
          Finally
  
              Try
                  If tcpClient IsNot Nothing Then
                      If tcpClient.Connected Then
                          tcpClient.Close()
                          tcpClient = Nothing
                      End If
                  End If
              Catch ex As Exception
              End Try
  
          End Try
  
      End While
  
  End Sub
  </code></pre>