title: Don't understand OpenSSL API error handling
body: <p>I'm implementing a little HTTP client using OpenSSL, and I'm trying to handle &quot;connection timed out&quot; errors gracefully.  By gracefully, I mean I want to print a nice, human-readable message that says &quot;Connection Timed Out.&quot;  Unfortunately, the error handling in OpenSSL isn't making sense to me.  Here's what happens:</p>
  <p>I create a nonblocking socket and deliberately connect to a port that I know won't respond, in order to test the error handling of &quot;connection timed out.&quot;  I make the socket into a nonblocking SSL channel.</p>
  <p>Then, I call SSL_connect.  It returns -1 as expected.  I call SSL_get_error to get more information about the error.  It returns SSL_ERROR_WANT_WRITE, as expected: it's waiting for connection to timeout, and that takes a while.  So far so good.</p>
  <p>I keep calling SSL_connect until finally, SSL_get_error returns SSL_ERROR_SYSCALL.  Again, this is what I expect.  I am expecting the connect system call to fail.  So far so good.</p>
  <p>Finally, and this is the part that isn't working for me, I try to get the actual error message.  Here is the code I'm using:</p>
  <pre><code>        unsigned long code =
              ERR_get_error_line_data(&amp;file, &amp;line, &amp;data, &amp;flags);
  </code></pre>
  <p>To my surprise, this is returning zero, meaning there are no more errors in the error queue.  I wasn't expecting that.  What I was expecting was an error code with the property ERR_GET_REASON(code) == ETIMEDOUT, so that I could then pass the ETIMEDOUT to strerror, to get the actual error message.  It seems weird to me that there's nothing at all in the error queue.  I don't understand that.</p>