Fix negotiation in stage 10 error

The problem was caused by a failed write to a non-blocking socket.
Problem fixed by using select() to provide a timeout, and retrying
writes if needed.