When I close the socket on one end of a connection, the other end gets an error the second time it sends data, but not the first time:
import socket server
As per the docs, try calling sock.shutdown() before the call to sock.close().
sock.shutdown()
sock.close()