title: bind() before connect() may return EADDRNOTAVAIL, even if there's many ephemeral ports available
body: <p>(This has been tested on Centos 6.5 and Centos 7)</p>
  
  <p>If you call bind() before connect() in linux with INADDR_ANY and port of 0 then the ephemeral port that bind() 
  chose may conflict with an established connection or a connection in TIME_WAIT and connect() will return EADDRNOTAVAIL. This is with sockets that have SO_REUSEADDR set. 
  This does not happen if you call connect() without bind(), because connect() has its 
  own (more sophisticated) method of choosing an ephemeral port.</p>
  
  <p>This seems consistent with the discussions here:</p>
  
  <p><a href="http://mail-archives.apache.org/mod_mbox/trafficserver-dev/201210.mbox/%3C001601cda670" rel="nofollow">http://mail-archives.apache.org/mod_mbox/trafficserver-dev/201210.mbox/%3C001601cda670</a>$fc59e470$f50dad50$@yooser.com%3E</p>
  
  <p>And here:</p>
  
  <p><a href="http://aleccolocco.blogspot.com/2008/11/ephemeral-ports-problem-and-solution.html" rel="nofollow">http://aleccolocco.blogspot.com/2008/11/ephemeral-ports-problem-and-solution.html</a></p>
  
  <p>I have seen this occur under high load with only a few existing established connections 
  to a destination IP, so this isn't really about using all of the available ephemeral ports 
  or changing the available ephemeral port ranges.</p>
  
  <p>The simple workaround (as discussed on the apache list) is to simply not call bind() before 
  connect(), so that connect()'s more sophisticated ephemeral port selection can be 
  used. </p>
  
  <p>But this is not always possible. What if your application optionally allows configuration of an 
  explicit port to bind to for outgoing connections, or a pool of IPs?</p>
  
  <p>Any other workarounds?</p>