@Override
	public void run()
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str;
		DatagramSocket datagramSocket = null;
		
		try {
			datagramSocket = new DatagramSocket();
			
			while ((str = br.readLine()) != null) {
				
				sentCount++;
				
				byte[] buffer = (processId + ":" + sentCount).getBytes();
				
				// Localhost : all processes on the same machine
				InetAddress address = InetAddress.getByName("localhost");
				
				DatagramPacket packet = new DatagramPacket(buffer, 
						   buffer.length,
						   address,
						   9007);

				while (!listener.acknowledgementReceived(7, sentCount)) {
					datagramSocket.send(packet);
					Thread.sleep(1000);
				}
				
				// The first part of the list is the message, to which we append the processId to identify the
				// sender at the receiver
				buffer = (str + ":" + processId + ":" + sentCount + ":" + listener.priority).getBytes();

				boolean allReceived = false;
				while (!allReceived) {
					for (int i : processToPort.keySet()) {
						if (!listener.acknowledgementReceived(i, sentCount)) {
							packet = new DatagramPacket(buffer, 
									   buffer.length,
									   address,
									   processToPort.get(i));
							
							//Drop messages with the probability specified in the input
							double toSend = Math.random();
							if (toSend > probability) {
								
								//Introducing a random delay in the range 0, 2 * delay
								Thread.sleep((long) Math.ceil((Math.random() * delay * 2)));
								
								datagramSocket.send(packet);
							}
						}
					}
					
					Thread.sleep(1000);
					
					allReceived = true;
					for (int i : processToPort.keySet()) {
						if (!listener.acknowledgementReceived(i, sentCount)) {
							allReceived = false;
						}
					}
				}
			}	
		} catch (Exception e) {
			System.out.println(e);
		} finally {
			datagramSocket.close();
		}
	}