import socket
import pickle


def send_packet(sock, pack):
    """
    Send a packet to remote socket. We first send
    the size of packet in bytes followed by the
    actual packet. Packet is serialized using
    cPickle module.

    Arguments
    ---------
    sock  : Destination socket
    pack  : Instance of class Packet.
    """
    if pack is None or (sock is None or type(sock) != socket.socket):
        return  # Nothing to send
    pack_raw_bytes = pickle.dumps(pack)
    dsize = len(pack_raw_bytes)
    sock.sendall(dsize.to_bytes(4, byteorder="big"))
    sock.sendall(pack_raw_bytes)
    return True