public Object receive() throws IOException, ClassNotFoundException {

		int lastAck = 0;
		while (true) {
			/** receive the datagrams and check checksum first  **/
			Datagram gram = datagramService.receiveDatagram();
			
			if (verbose > 0) {
				System.out.println("received datagram, sequenceNum: " + String.valueOf(gram.getSeq()));
			}
			
			short checksum = gram.getChecksum();
			
			if (!validateChecksum(gram.getData(), checksum)) {
				System.out.println("Checksum error, datagram sequenceNum: " + String.valueOf(gram.getSeq()));
				break;
			}
			
			/** if this is the first gram, add it directly
			 	if this is not the first one, the only condition it can be added to the list is
			 	the equation of seq and the expected ack **/
			if (receivedList.isEmpty() || gram.getSeq() == lastAck) {
				receivedList.add(gram);
				lastAck = gram.getSeq() + gram.getSize();
			}
			
			/** build a return gram and fill the Ack field **/
			Datagram returnGram = new Datagram();
			returnGram.setSrcaddr(this.srcIP);
			returnGram.setDstaddr(this.desIP);
			returnGram.setSrcport(this.srcPort);
			returnGram.setDstport(this.desPort);
			returnGram.setAck(lastAck);
			returnGram.setFrag(gram.getFrag());
			returnGram.setChecksum(makeChecksum(returnGram));

			datagramService.sendDatagram(returnGram);

			/** if this gram is the last segment, break the loop **/
			if (gram.getFrag() == false) {
				break;
			}
		}

		/** reassemble the grams in the list **/
		Datagram datagram = reassemble(receivedList);
		
		return datagram.getData();
	}