add a wait to the loop in close

this is possibly blocking in cases where the connection is now unresponsive, and also causing timeouts which wrap this to block.
Merge pull request #108 from joshk/wait_during_close_loop

Add a wait to the loop in close