/// <summary>
        /// Reconstruct a file packet by packet, when the last packet is receive the file is created.
        /// </summary>
        /// <param name="newPacket"></param>
        public void ReconstructFile(Packet newPacket)
        {
            ReconstructedPackets.Add(newPacket);

            //Console.WriteLine("Reconstruct Packet ID " + newPacket.aFrame.aHeader.FileID);

            //last packet, and we received all the packets.
            if (newPacket.aFrame.aHeader.PacketNumber == newPacket.aFrame.aHeader.TotalNbPackets - 1 &&
                ReconstructedPackets.Count >= newPacket.aFrame.aHeader.TotalNbPackets)
            {
               // Console.WriteLine("Number of packet received:" + ReconstructedPackets.Count);
                //Sanity check on packet
                for (int i = 0; i < newPacket.aFrame.aHeader.TotalNbPackets; i++)
                {
                    if (((Packet)ReconstructedPackets[i]).aFrame.aHeader.FileID != newPacket.aFrame.aHeader.FileID)
                    {
                        ReconstructedPackets.Clear();

                        return;
                    }
                }

                d = new byte[newPacket.aFrame.aHeader.TotalNbPackets * Packet.DEFAULT_PACKET_SIZE];

                for (int i = 0; i < newPacket.aFrame.aHeader.TotalNbPackets; i++)
                {
                    Buffer.BlockCopy(((Packet)ReconstructedPackets[i]).aFrame.Data,
                                        0,
                                        d,
                                        i * Packet.DEFAULT_PACKET_SIZE,
                                        ((Packet)ReconstructedPackets[i]).aFrame.aHeader.DataUsed);

                }

                if (aPacketReconstructedCBHandler != null)
                {
                    aPacketReconstructedCBHandler(newPacket.aFrame.aHeader.FileID, d, d.Length);
                }

                d = null;
                ReconstructedPackets.Clear();

            }
        }
        //Handles the reception of 1 UDP packet of data.
        public void ReceivedHandler(int NumberOfAvailableData)
        {
            int PacketSize = Packet.GetHeaderSize() + Packet.DEFAULT_PACKET_SIZE;

            if (NumberOfAvailableData >= PacketSize)
            {

                while (aUDPListener.GetNumberOfReceivedData() >= PacketSize)
                {
                    byte[] buffer = new byte[PacketSize];

                    aUDPListener.ReceiveData(ref buffer, PacketSize);

                    Packet p = new Packet(buffer);

                    PacketReconstructors.ReconstructFile(p);

                }

            }
        }
        /// <summary>
        /// Partition a file in packets and adds the packet to the arraylist.
        /// </summary>
        /// <param name="Data"></param>
        /// <param name="FileSize"></param>
        public void PartitionFile(byte[] Data, int FileSize)
        {
            //Clear previous packets in the arraylist
            PartitionnedPackets.Clear();

            //Calculate the number of full packets
            int NbFullPkt = FileSize / Packet.DEFAULT_PACKET_SIZE;
            int NbRemainingPkt = FileSize % Packet.DEFAULT_PACKET_SIZE;
            int TotalNbOfPackets;

            if(NbRemainingPkt > 0)
            {
                TotalNbOfPackets = NbFullPkt + 1;
            }
            else
            {
                TotalNbOfPackets = NbFullPkt;
            }

            for (int i = 0; i < NbFullPkt; i++)
            {
                byte[] d = new byte[Packet.DEFAULT_PACKET_SIZE];

                Buffer.BlockCopy(Data, i * Packet.DEFAULT_PACKET_SIZE, d, 0, Packet.DEFAULT_PACKET_SIZE);

                Packet p = new Packet(FileID, FileSize, i, TotalNbOfPackets, Packet.DEFAULT_PACKET_SIZE, d);

                PartitionnedPackets.Add(p);

                d = null;
            }

            if (NbRemainingPkt > 0)
            {
                byte[] d = new byte[Packet.DEFAULT_PACKET_SIZE];

                Buffer.BlockCopy(Data, NbFullPkt * Packet.DEFAULT_PACKET_SIZE, d, 0, NbRemainingPkt);

                Packet p = new Packet(FileID, FileSize, TotalNbOfPackets-1, TotalNbOfPackets, NbRemainingPkt, d);

                PartitionnedPackets.Add(p);

                d = null;
            }

            if (PacketPartitionnerCBHandler != null)
            {
                PacketPartitionnerCBHandler(PartitionnedPackets);
            }
        }