handshake implementation (authentication and establishment of encryption)