refactoring to share code between client and server handshakes