Handshaking with peers

Still ugly and untested... but it works.