Add a wait to the loop in close

This is possibly blocking in cases where the connection is now unresponsive, and also potentially blocking timeouts which wrap the calling of the close method.