Tweaked closing the connection to prevent a race condition between the actual close and the QUIT message
Abstracted the closing of the connection slightly