In the case of a failure, the socket is now explicitely closed. I used a try: finally: to achieve this.