Removed internal try\excepts and added the socket.close() to finally.