Added handling for closed sockets.

The trick here was figuring out when the socket got closed.