'Perform handshakes for TLS.
If the handshake is successful, the XML stream will need
to be restarted.'

def start_tls(self):

    log.info(u'Negotiating    TLS')
    ssl_socket = self._create_secure_socket()
    if hasattr(self.socket, u'socket'):
        self.socket.socket = ssl_socket
    else:
        self.socket = ssl_socket
    try:
        self.socket.do_handshake()
    except (Socket.error, ssl.SSLError):
        log.error(u'CERT:    Invalid    certificate    trust    chain.')
        if (not self.event_handled(u'ssl_invalid_chain')):
            self.disconnect(self.auto_reconnect, send_close=False)
        else:
            self._der_cert = self.socket.getpeercert(binary_form=True)
            self.event(u'ssl_invalid_chain', direct=True)
        return False
    self._der_cert = self.socket.getpeercert(binary_form=True)
    pem_cert = ssl.DER_cert_to_PEM_cert(self._der_cert)
    log.debug(u'CERT:    %s', pem_cert)
    self.event(u'ssl_cert', pem_cert, direct=True)
    try:
        cert.verify(self._expected_server_name, self._der_cert)
    except cert.CertificateError as err:
        if (not self.event_handled(u'ssl_invalid_cert')):
            log.error(err)
            self.disconnect(self.auto_reconnect, send_close=False)
        else:
            self.event(u'ssl_invalid_cert', pem_cert, direct=True)
    self.set_socket(self.socket)
    return True
