Based on code from: http://docs.python.org/2/library/socket.html
