public void read(SelectionKey key) throws IOException,
			ClassNotFoundException {
		loggerNetworkClient.info("READ:");
		
		SocketChannel socketChannel = (SocketChannel) key.channel();
		this.rBuffer.clear();

		

		int numRead = 0;

		try {
			numRead = socketChannel.read(rBuffer);
		} catch (Exception e) {
			numRead = -1000000000;
		}

		if (numRead <= 0) {
			loggerNetworkClient.info("The socket was closed by the remote client"
					+ key);
			key.channel().close();
			key.cancel();
			return;
		}

		byte[] rbuf = null;
		rbuf = readBuf;

		int rbuflen = 0;
		if (rbuf != null) {
			rbuflen = rbuf.length;
		}

		byte[] currentBuf = rBuffer.array();
		loggerNetworkClient.info("Number of read bytes " + numRead
				+ " associated with the key " + key + " : " + currentBuf);

		byte[] newBuf = new byte[rbuflen + numRead];

		/*
		 * Copy received data into newBuf rbuf - contains data previously
		 * received but incomplete currentBuf - contains data received during
		 * the current read
		 */
		for (int i = 0; i < rbuflen; i++) {
			newBuf[i] = rbuf[i];
		}

		for (int i = rbuflen; i < newBuf.length; i++) {
			newBuf[i] = currentBuf[i - rbuflen];
		}

		int i = 0;
		int length = 0;

		// Read size of sent object
		if (i + 4 > newBuf.length)
			return;

		length = NetworkPacketManager.getLength(new byte[] { newBuf[0],
				newBuf[1], newBuf[2], newBuf[3] });

		loggerNetworkClient.info("The length of the package = " + length);
		i += 4;

		// Read the serialized object
		if (i + length <= newBuf.length) {
			loggerNetworkClient.info("deserialize received object");

			Object st = NetworkPacketManager.deserialize(Arrays.copyOfRange(newBuf, 4, length + 4));
			
			ClientUtils.chooseAction(st);
			i += length;
		} else
			i -= 4;

		byte[] finalBuf = null;
		if (i > 0) {
			finalBuf = new byte[newBuf.length - i];
			for (int j = i; j < newBuf.length; j++) {
				finalBuf[j - i] = newBuf[j];
			}
		} else {
			finalBuf = newBuf;
		}

		readBuf = finalBuf;
	}