title: What could cause socket ConnectException: Connection timed out?
body: <p>We have a Webstart client that communicates to the server by sending serialized objects over HTTPS using <code>java.net.HttpsURLConnection</code>.</p>
  
  <p>Everything works perfectly fine on my local machine and on test servers located in our office, but I'm experiencing a very, very strange issue which is only occurring on our production and staging servers (and sporadically at that). The main difference I know of between those servers and the ones in our office is that they are located elsewhere and client-server communication with them is considerably slower, but it worked fine for a long time in production prior to this as well.</p>
  
  <p>Anyway, here's what's happening:</p>
  
  <ul>
  <li>The client, after setting options such as read timeout and properties such as <code>Content-Type</code> on the <code>HttpURLConnection</code>, calls <code>getOutputStream()</code> on it to get the stream to write to.</li>
  <li>At this point, from what I can tell, the client hangs for some period of time.</li>
  <li>The client then throws the following exception:</li>
  </ul>
  
  <pre>
  java.net.ConnectException: Connection timed out: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(Unknown Source)
      at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
      at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source)
      at sun.net.NetworkClient.doConnect(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.protocol.https.HttpsClient.(Unknown Source)
      at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
  </pre>
  
  <p>Note that this is not a <code>SocketTimeoutException</code>, which the <code>connect()</code> method on <code>HttpURLConnection</code> says it throws if the timeout expires before a connection can be established. Also, when this happens I am able to call <code>conn.getResponseCode()</code> and I get a response code of 200.</p>
  
  <ul>
  <li>On the server side, an <code>EOFException</code> is thrown in <code>ObjectInputStream</code>'s constructor, which tries to read the serialization header but fails because the client never gets the <code>OutputStream</code> to write to.</li>
  </ul>
  
  <p>In case it helps, here are the calls being made on the <code>HttpsURLConnection</code> prior to the call to <code>getOutputStream()</code> (edited to show only the calls being made rather than the whole structure of the code doing this):</p>
  
  <pre><code>HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
  conn.setUseCaches(false);
  conn.setReadTimeout(30000);
  conn.setRequestProperty("Cookie", cookie);
  conn.setDoOutput(true);
  conn.setRequestProperty("Content-Type", "application/x-java-serialized-object");
  conn.getOutputStream();
  </code></pre>
  
  <p>The thing is, I have no idea how any of this could be happening, especially given that it only happens <em>occasionally</em> (no clear pattern of activity that I can tell) and even then only when there's (relatively) high latency between the client and the server.</p>
  
  <p>Given what I've been able to find so far about <code>java.net.ConnectException: Connect timed out</code>, I wondered if it weren't some network or firewall issue on the network our servers are running on... but that doesn't make much sense to me given that the request is clearly getting through to the servlet. Also, other apps running on the same network have not reported similar issues.</p>
  
  <p>Does anyone have any idea what the cause of this could be, or even what I should investigate? </p>