Fixed socket.close exception, closes #859

The call to socket.socket.close(self) raised an exception in at least some conditions.
Merge pull request #860 from max0d41/patch-1

Fixed socket.close exception, closes #859