/**
         * Allows to receive a generic byte stream and convert it in a specific packet object
         * Returns NULL if the received stream doesn't correspond with a known Transmission Packet
         */

        public TransmissionPacket receivePacket(Socket handler)
        {
            string             data     = "";
            TransmissionPacket received = null;
            int     bytesRec;
            JObject o = null;

            while (!(handler.Poll(1, SelectMode.SelectRead) && handler.Available == 0))
            {
                byte[] bytes = new byte[1024];
                bytesRec = handler.Receive(bytes);
                data    += Encoding.UTF8.GetString(bytes, 0, bytesRec);
                //Console.WriteLine(data);
                if (data.IndexOf("<EOF>") > -1)
                {
                    break;
                }
            }

            data = data.Substring(0, data.Length - 5);      //remove the end_of_file

            o = (JObject)JsonConvert.DeserializeObject(data);

            if (o["Type"].ToString() == TransmissionPacket.PacketType.request.ToString())                 // REQUEST
            {
                received = JsonConvert.DeserializeObject <RequestPacket>(data);
            }
            else if (o["Type"].ToString() == TransmissionPacket.PacketType.response.ToString())           // RESPONSE
            {
                received = JsonConvert.DeserializeObject <ResponsePacket>(data);
            }

            return(received);
        }
        public override TransferResult transfer()
        {
            ResponsePacket     response = null;
            TransmissionPacket tPacket  = null;

            try {
                byte[] msg = network.generateRequestStream(job.Task);   // Encode the data string into a byte array.

                int bytesSent = network.SendPacket(socket, msg);        // Send the data through the socket.

                job.Status = Job.JobStatus.WaitingForRemoteAcceptance;

                tPacket = (TransmissionPacket)network.receivePacket(this.socket);       // Receive the response from the remote.
                if (tPacket.Type.ToString() != "response")
                {
                    throw new SocketException();//TODO This must raise an exception cause we don't expect a server to send packets different from Responses at this point
                }
                response = (ResponsePacket)tPacket;

                job.Status = Job.JobStatus.Active;

                //Logger.log(Logger.TRANSFER_CLIENT_DEBUG, "Handshake competed. Starting transmission of the file " + job.Task.Info.Name + "\n");

                if (response.Procede)
                {
                    try {
                        protocol.enterClient();

                        int i = 0;
                        while (iterator.hasNext())
                        {
                            if (job.Status != Job.JobStatus.Active)
                            {
                                throw new SocketException();
                            }
                            i = iterator.next(transferBlock);

                            // If the server closes the socket, this instruction will throw a SocketException
                            socket.Send(transferBlock, 0, i, SocketFlags.None);
                        }

                        //Logger.log(Logger.TRANSFER_CLIENT_DEBUG, "Transfer completed for the file " + job.Task.Info.Name + "\n");

                        Logger.log(Logger.TRANSFER_CLIENT_DEBUG, "Releasing lock\n");

                        job.Status = Job.JobStatus.Completed;
                    } finally {
                        ///* Close the file iterator releasing resources and
                        // * releases protocol resources */
                        //iterator.close();

                        if (job.Status == Job.JobStatus.Active) // If job status is still active it was not me to raise this error
                        {
                            job.Status = Job.JobStatus.ConnectionError;
                        }

                        // I don't know if I really have acquired a slot (maybe an exception occourde befor I could do it)
                        // The realease operation will throw an exception if the I have not acquired a slot
                        try {
                            // Release the slot
                            protocol.releaseClient();
                        } catch (System.Threading.SemaphoreFullException e) {
                            // This means that SocketException occurred befor I could acquire the semaphore slot
                        }
                    }
                }
                else
                {
                    ///* Close the file iterator releasing resources and
                    // * releases protocol resources */
                    //iterator.close();
                    job.Status = Job.JobStatus.NotAcceptedByRemote;
                }
            } finally {
                /* Close the file iterator releasing resources and
                 * releases protocol resources */
                iterator.close();
            }

            return(new TnSTransferResult(response.Procede));
        }