handshake protocol for reading and writing