stop session immediately if handshake fails; move some code