bugfix: Check for sockets before closing.