In my Python socket program, I sometimes need to interrupt it with Ctrl-C. When I do this, it does close the connection using socket.close().
socket.close()
A simple solution that worked for me is to close the Terminal and restart it.