Implements handshake and basic protocol