'Closes a socket instance cleanly
:param sock: the network socket to close
:type sock: socket.socket'

def close_socket(self, sock):

    try:
        sock.shutdown(socket.SHUT_RDWR)
    except socket.error as e:
        if (e.errno == errno.EBADF):
            return
        if ((e.errno != errno.ENOTCONN) or (platform.system() not in ['Darwin', 'Windows'])):
            raise
    sock.close()
