title: OpenSSL error handling
body: <p>Is this the correct way to do error handling in OpenSSL?
  And what is the difference between <code>SSL_get_error</code> and <code>ERR_get_error</code>?
  The docs are quite vague in this regard.</p>
  
  <pre><code>int ssl_shutdown(SSL *ssl_connection)
  {
      int rv, err;
      ERR_clear_error();
      rv = SSL_shutdown(ssl_connection);
  
      if (rv == 0)
          SSL_shutdown(ssl_connection);
  
      if (rv &lt; 0)
      {
          err = SSL_get_error(ssl_connection, rv);
  
          if (err == SSL_ERROR_SSL)
              fprintf(stderr, "%s\n", ERR_error_string(ERR_get_error(), NULL));
  
          fprintf(stderr, "%s\n", SSL_state_string(ssl_connection));
  
          return 1;
      }
  
      SSL_free(ssl_connection);
      return 0;
  }
  </code></pre>