title: Why is it that Windows Java cannot bind to a port that has already been bound?
body: <p>We have the need to bind to the same local port when responding to requests.
  Below is a sample of Java code. On Linux we can bind to the same port twice,
  but on Windows we get error on second bind.  The error is BindException: Address already in use. A similar program written in C allows us to bind twice.
  What is preventing Java on Windows from doing this?  Is there a registry setting to allow the bind?</p>
  
  <pre><code>import java.net.InetSocketAddress;
  import java.net.Socket;
  import java.net.SocketAddress;
  
  public class SocketTest 
  {
      public static void main(String[] args) throws Exception
      {
          Socket s1 = new Socket();
          s1.setReuseAddress(true);
          SocketAddress saremote = new InetSocketAddress("google.com", 80);
          SocketAddress salocal = new InetSocketAddress("10.1.1.98", 9990);
          s1.bind(salocal);
          s1.connect(saremote);
  
          Socket s2 = new Socket();
          s2.setReuseAddress(true);
          SocketAddress saremote2 = new InetSocketAddress("yahoo.com", 80);
          SocketAddress salocal2 = new InetSocketAddress("10.1.1.98", 9990);
          s2.bind(salocal2);
          s2.connect(saremote2); 
      }
  }
  </code></pre>