use defer for handshake function call