code: def connect(self):
          
          sock = socket.create_connection((self.host, self.port), self.timeout)
          if not USE_STDLIB_SSL:
              ssl_ctx = configure_pyopenssl_context(self.credentials)
  
              # attempt to upgrade the socket to TLS
              cxn = OpenSSL.SSL.Connection(ssl_ctx, sock)
              cxn.set_connect_state()
              while True:
                  try:
                      cxn.do_handshake()
                  except OpenSSL.SSL.WantReadError:
                      select.select([sock], [], [])
                      continue
                  except OpenSSL.SSL.Error as e:
                      raise SecurityError('bad handshake - ' + str(e))
                  break
  
              self.sock = RiakWrappedSocket(cxn, sock)
              self.credentials._check_revoked_cert(self.sock)
          else:
              ssl_ctx = configure_ssl_context(self.credentials)
              if self.timeout is not None:
                  sock.settimeout(self.timeout)
              self.sock = ssl.SSLSocket(sock=sock,
                                        keyfile=self.credentials.pkey_file,
                                        certfile=self.credentials.cert_file,
                                        cert_reqs=ssl.CERT_REQUIRED,
                                        ca_certs=self.credentials.cacert_file,
                                        ciphers=self.credentials.ciphers,
                                        server_hostname=self.host)
              self.sock.context = ssl_ctx
docs: Connect to a host on a given (SSL) port using PyOpenSSL.