title: Apache http client -> local address bind -> BindException after some time
body: <p>OS: Linux ( Debian )<br />
  Java: Java(TM) SE Runtime Environment (build 1.7.0_05-b06) <br />
  Java: Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)<br />
  Apache http client: v4.2.3 ( latest )</p>
  
  <p>I want to create one PoolingConnectionManager with several clients, each with its unique local address (ConnRoutePNames.LOCAL_ADDRESS).<br />
  Those clients will be used by several workers ( each worker selects random client and executes request ).<br /></p>
  
  <p><strong>The problem is</strong>: When i set local address for clients, after some time (e.g. 1 min) i always get java.net.BindException with message "Address already in use".<br /></p>
  
  <p><strong>Question</strong>: Is it bug?</p>
  
  <pre><code>java.net.BindException: Address already in use
  at java.net.PlainSocketImpl.socketBind(Native Method) ~[na:1.7.0_05]
  at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376) ~[na:1.7.0_05]
  at java.net.Socket.bind(Socket.java:627) ~[na:1.7.0_05]
  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:120) ~[httpclient-4.2.3.jar:4.2.3]
  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) ~[httpclient-4.2.3.jar:4.2.3]
  at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294) ~[httpclient-4.2.3.jar:4.2.3]
  at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645) ~[httpclient-4.2.3.jar:4.2.3]
  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480) ~[httpclient-4.2.3.jar:4.2.3]
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) ~[httpclient-4.2.3.jar:4.2.3]
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) ~[httpclient-4.2.3.jar:4.2.3]
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784) ~[httpclient-4.2.3.jar:4.2.3]
  at controllers.Test$Worker.run(Test.java:67) ~[test_2.9.1-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
  at java.lang.Thread.run(Thread.java:722) [na:1.7.0_05]
  </code></pre>
  
  <p>Code ( simplified ):</p>
  
  <pre><code>public static void main(String[] args) throws UnknownHostException
  {
      ClientConnectionManager connManager = buildConnectionManager(30);
  
      List&lt;HttpClient&gt; clients = new ArrayList&lt;&gt;();
      for(int i=0; i&lt;8; ++i)
      {
          HttpClient client = buildClient(connManager, InetAddress.getByName("111.111.111." + (50 + i));
          clients.add(client);
      }
  
      for(int i=0; i&lt;30; ++i)
      {
          new Thread(new Worker(clients)).start();
      }
  }
  
  public static ClientConnectionManager buildConnectionManager(Integer parallelism)
  {
      PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
      connectionManager.setMaxTotal(parallelism);
      connectionManager.setDefaultMaxPerRoute(parallelism);
  
      return connectionManager;
  }
  
  public static HttpClient buildClient(ClientConnectionManager connectionManager, InetAddress localAddress)
  {
      DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
      HttpParams params = httpClient.getParams();
      params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);
  
      return httpClient;
  }
  
  private static class Worker implements Runnable
  {
      private List&lt;HttpClient&gt; clients = null;
  
      public Worker(List&lt;HttpClient&gt; clients)
      {
          this.clients = clients;
      }
  
      public void run()
      {
  
          do
          {
              HttpGet httpGet = new HttpGet("http://google.com/robots.txt");
              HttpClient client = this.clients.get(new Random().nextInt(this.clients.size()));
  
              try
              {
                  HttpResponse httpResponse = client.execute(httpGet);
                  EntityUtils.consume(httpResponse.getEntity());
  
                  logger.debug("Success request");
              }
              catch(IOException e)
              {
                  httpGet.abort();
                  logger.info("IO error", e);
              }
          }
          while(true);
      }
  }
  </code></pre>