public void receive(){
		boolean tcpComplete = false;
		ArrayList<byte[]> fileParts = new ArrayList<byte[]>();
		byte responseBuffer[];
		DatagramPacket responsePacket;
		Packet responsePacketData;
		int currentSequenceNumber = 1;
		
		try {
			if(logFileName.equals("stdout")){
				this.logger = new PrintWriter(System.out);
			}
			else{
				this.logger = new PrintWriter(new BufferedWriter(new FileWriter(this.logFileName, true)));
			}
			while(!tcpComplete){	
				packetSocket.receive(bufferPacket);
				logPacket(bufferPacket, false);
				if (Packet.getPurpose(bufferPacket.getData()[12]) == "FIN"){
					tcpComplete = true;
				}
				//Structure needs refactoring
				else if (Packet.getSequenceNumber(bufferPacket.getData()) != currentSequenceNumber){//wrong packet sequence number
					//send ACK 
					responsePacketData = new Packet(listenPort, remotePort, 0, currentSequenceNumber, "ACK", null);
					responseBuffer = responsePacketData.getPacketLoad();
					responsePacket = new DatagramPacket(responseBuffer, responseBuffer.length, remoteIP, remotePort);
					packetSocket.send(responsePacket);
					logPacket(responsePacket, true);
				}
				else if (Packet.isCorrupt(bufferPacket.getData())){//is corrupt data packet
					//send CORR response
					responsePacketData = new Packet(listenPort, remotePort, 0, 0, "CORR", null);
					responseBuffer = responsePacketData.getPacketLoad();
					responsePacket = new DatagramPacket(responseBuffer, responseBuffer.length, remoteIP, remotePort);
					packetSocket.send(responsePacket);
					logPacket(responsePacket, true);
				}
				else{//Packet is not corrupt and is correct one
					fileParts.add(bufferPacket.getData());
					currentSequenceNumber = Packet.getACKNumber(bufferPacket.getData());
					//send ACK response 
					responsePacketData = new Packet(listenPort, remotePort, 0, currentSequenceNumber, "ACK", null);
					responseBuffer = responsePacketData.getPacketLoad();
					responsePacket = new DatagramPacket(responseBuffer, responseBuffer.length, remoteIP, remotePort);
					packetSocket.send(responsePacket);
					logPacket(responsePacket, true);
				}				
				//flush buffer and packet
				this.buffer = new byte[576];
				this.bufferPacket = new DatagramPacket(buffer, buffer.length);
			}   
		}
		catch (IOException e) {
			System.out.println("Error has occurred with packet receiving.");
		} catch (NoSuchAlgorithmException e) {
			System.out.println("Error has occurred with checksum calculations.");
		}	
		compileFile(fileParts);
		if(!logFileName.equals("stdout")){
			logger.close();
		}
		packetSocket.close();
		System.out.println("    Delivery completed successfully");
	}