title: Java Client Server Application - Address already in use: connect
body: <p><br>
  I am currently working on a simple multiplayer game where serveral clients need to connect to a server.
  <p> 
  My server consits of a single serverSocket. This serverSocket accepts incoming connections and hands them over to connection object that starts a separate thread. 
  <p> </p>
  
  <pre><code>ServerSocket seso = new ServerSocket(12345);
  
  while(true){
      Socket toClient = seso.accept();                
      new Connection(toClient); //creates a thread that opens streams etc
  }
  </code></pre>
  
  <p><p>
  Clients open a new Socket and connect to this server. 
  <p></p>
  
  <pre><code>Socket toServer = new Socket();
  toServer.setReuseAddress(true);
  toServer.bind(new InetSocketAddress(65432)); //always using the same port   
  toServer.connect(new InetSocketAddress(serverIP,12345));
  </code></pre>
  
  <p>Now if i close the connection to the server using <code>toServer.close();</code> and try to connect again to the server, i get an "address already in use: connect" exception. 
  <p>
  Using TCPView i can see that the state of the client procress changes to TIME_WAIT. But shouldn't i be able to use this port again because of <code>setReuseAddress(true)</code>? Am i using it wrong or is it an server problem? 
  <p>
  I do always call .close() on toClient and toServer. Nevertheless i always have to wait until the socket is completely closed (after TIME_WAIT) before this client can connect again to the server. 
  <p>
  When i close the entire application, the socket is immediately closed (not in state TIME_WAIT) and this client can connect to my server. (And ofc there is a connection reset exception in my server)
  <p></p>
  
  <p>How can I do that without always closing the application ? <br>
  Thanks for your help.</p>