'Create SSL socket and connect to peer'

def connect(self):

    if getattr(self, 'ssl_context', None):
        if (not isinstance(self.ssl_context, SSL.Context)):
            raise TypeError(('Expecting    OpenSSL.SSL.Context    type    for    "ssl_context"    attribute;    got    %r    instead' % self.ssl_context))
        ssl_context = self.ssl_context
    else:
        ssl_context = SSL.Context(self.__class__.default_ssl_method)
    sock = socket.create_connection((self.host, self.port), self.timeout)
    if getattr(self, '_tunnel_host', None):
        self.sock = sock
        self._tunnel()
    self.sock = SSLSocket(ssl_context, sock)
    self.sock.set_connect_state()
