def enable_ssl(self, raw_socket, ssl_options):
    self._logger.debug('=> %s', messages.SslRequest())
    raw_socket.sendall(messages.SslRequest().get_message())
    response = raw_socket.recv(1)
    self._logger.debug('<= SslResponse: %s', response)
    if (response == b'S'):
        self._logger.info('Enabling SSL')
        try:
            if isinstance(ssl_options, ssl.SSLContext):
                server_host = self.address_list.peek_host()
                if (server_host is None):
                    msg = 'Cannot get the connected server host while enabling SSL'
                    self._logger.error(msg)
                    raise errors.ConnectionError(msg)
                raw_socket = ssl_options.wrap_socket(raw_socket, server_hostname=server_host)
            else:
                raw_socket = ssl.wrap_socket(raw_socket)
        except ssl.CertificateError as e:
            raise_from(errors.ConnectionError(str(e)), e)
        except ssl.SSLError as e:
            raise_from(errors.ConnectionError(str(e)), e)
    else:
        err_msg = 'SSL requested but not supported by server'
        self._logger.error(err_msg)
        raise errors.SSLNotSupported(err_msg)
    return raw_socket
def enable_ssl(self, raw_socket, ssl_options):
    self._logger.debug('=> %s', messages.SslRequest())
    raw_socket.sendall(messages.SslRequest().get_message())
    response = raw_socket.recv(1)
    self._logger.debug('<= SslResponse: %s', response)
    if (response == b'S'):
        self._logger.info('Enabling SSL')
        try:
            if isinstance(ssl_options, ssl.SSLContext):
                server_host = self.address_list.peek_host()
                if (server_host is None):
                    msg = 'Cannot get the connected server host while enabling SSL'
                    self._logger.error(msg)
                    raise errors.ConnectionError(msg)
                raw_socket = ssl_options.wrap_socket(raw_socket, server_hostname=server_host)
            else:
                raw_socket = ssl.wrap_socket(raw_socket)
        except ssl.CertificateError as e:
            raise_from(errors.ConnectionError(str(e)), e)
        except ssl.SSLError as e:
            raise_from(errors.ConnectionError(str(e)), e)
    else:
        err_msg = 'SSL requested but not supported by server'
        self._logger.error(err_msg)
        raise errors.SSLNotSupported(err_msg)
    return raw_socket