Fix for Issue #356.

There is probably a better approach to fixing this, maybe using self.is_closed instead of self.socket.

Another approach would be to make sure that the socket is not nullified before reaching the close function.