title: Java HTTPS Connection Forwarding
body: <p>I'm writing a simple program in <code>Java</code> that receives connection requests from a browser (like Firefox), parses the request for statistical info, then forwards the request to the original destination. The program then reads the response from the destination, parses the response for statistical info, then forwards the response to the browser.</p>
  
  <p>The pseudo code of this operation is as follows:</p>
  
  <pre class="lang-java prettyprint-override"><code>// Accept connection from browser and read request
  1. Socket browserConnection = serverSocket.accept();
  2. browserConnection.getInputStream().read(buffer);
  3. SocketInetAddress destInetAddress = parseHttpRequest(buffer);
  
  // Connect to destination and forward request
  4. Socket destConnection = new Socket(destInetAddress);
  5. destConnection.getOutputStream().write(buffer);
  
  // Read response from destination
  6. destConnection.getInputStream().read(buffer);
  7. parseHttpResponse(buffer);
  
  // Forward response to browser
  8. browserConnection.getOutputStream().write(buffer);
  </code></pre>
  
  <p>This works well with HTTP connections, but I'm getting <code>connection reset</code> for HTTPS connections.</p>
  
  <p><strong>NOTE :</strong> I know the difference between HTTP and HTTPS connections, that unlike HTTP, it's NOT just a one-time <code>send</code> and then some <code>receives</code>. My code for HTTPS <code>reads</code> as much as needed, and also <code>writes</code> as much as needed.</p>
  
  <p>Why am I getting connection resets from any HTTPS server (e.g. <a href="https://www.google.com" rel="nofollow">https://www.google.com</a>, <a href="https://www.comodo.com" rel="nofollow">https://www.comodo.com</a>, etc.) I try to connect ?!</p>