simplify, using socket.sendall instead of send
more tests and some refactoring