ported V3 handshake; needs cleanup, documentation, and error-checking