'Close the connection without assuming anything about it.'

def close(self):

    try:
        file = self.file
        self.file = None
        if (file is not None):
            file.close()
    finally:
        sock = self.sock
        self.sock = None
        if (sock is not None):
            try:
                sock.shutdown(socket.SHUT_RDWR)
            except OSError as exc:
                if ((exc.errno != errno.ENOTCONN) and (getattr(exc, 'winerror', 0) != 10022)):
                    raise
            finally:
                sock.close()
