title: Timeout SSL handshake
body: <p>I'm trying to incorporate <code>TimeoutMixin</code> in a protocol over SSL.  However, when the timeout occurs and it makes a call to the <code>transport.loseConnection()</code> nothing happens.  I think this is related to <a href="http://twistedmatrix.com/trac/browser/tags/releases/twisted-14.0.2/twisted/protocols/tls.py#L425" rel="nofollow">this code in TLSMemoryBIOProtocol</a>:</p>
  
  <pre><code>def _shutdownTLS(self):
      """
      Initiate, or reply to, the shutdown handshake of the TLS layer.
      """
      try:
          shutdownSuccess = self._tlsConnection.shutdown()
      except Error:
          # Mid-handshake, a call to shutdown() can result in a
          # WantWantReadError, or rather an SSL_ERR_WANT_READ; but pyOpenSSL
          # doesn't allow us to get at the error.  See:
          # https://github.com/pyca/pyopenssl/issues/91
          shutdownSuccess = False
      self._flushSendBIO()
      if shutdownSuccess:
          # Both sides have shutdown, so we can start closing lower-level
          # transport. This will also happen if we haven't started
          # negotiation at all yet, in which case shutdown succeeds
          # immediately.
          self.transport.loseConnection()
  </code></pre>
  
  <p>The issue is that the time-out is happening before the handshaking can occur.  On the server side it has a port open listening for connections but the server is frozen and can't do the proper handshaking.  That code snippet looks like it fails to do the TLS shutdown and then does nothing.</p>
  
  <h2>My question is:</h2>
  
  <p>How do I set a timeout on the SSL handshaking?  If the handshaking doesn't happen in a reasonable amount of time, how do drop the connection properly?  Also, is there anything wrong with the above snippet being changed to drop the underlying lower-level connection regardless of the severing of the TLS connection?  (just doing nothing and hanging indefinitely doesn't seem like the right approach)</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The failure of the call to <code>loseConnection</code> seems to happen if any data is sent before and if nothing is sent than it seems to work properly.</p>