public void run() {

		while (((serverRunning) || (clientConnected)) && (!terminate)) {

			try {

				if (serverRunning) {
					Socket conn = null;
					conn = server.accept();
					System.out.println("Connection established!");
					outStream =
							new BufferedOutputStream(conn.getOutputStream());
					inStream =
							new BufferedInputStream(conn.getInputStream());
				}


				int data = 0, initial, clock, b1, b2, b3, b4, v = 0;
				while ((data != -1) && (!terminate)) {           /* This needs to terminate */

					if (dmgcpu != null) {
						v = dmgcpu.getInstrCount();
					}

					initial = inStream.read();

					b1 = inStream.read();
					b2 = inStream.read();
					b3 = inStream.read();
					b4 = inStream.read();

					//    System.out.println(b1+" "+b2+" "+b3+" "+b4);

					clock = b1 + (b2 << 8) + (b3 << 16) + (b4 << 24);

					data = inStream.read();
					//    System.out.println("Synched: " + clock + " : " + dmgcpu.instrCount);

					//    System.out.println(v + " " + dmgcpu.instrCount);
					if (dmgcpu != null) {
						while ( /*(v + 600 > dmgcpu.instrCount) || /*((clock != -1) && (clock > dmgcpu.instrCount)) ||*/ (!dmgcpu.interruptsEnabled) || ((dmgcpu.ioHandler.getRegisters()[0x0F] & dmgcpu.INT_SER) != 0) && (!terminate)) {
							try {
								java.lang.Thread.sleep(5);
							} catch (InterruptedException e) {

							}
						}
					}


					if (initial == 1) {
						//      System.out.print("<-- " + data + ":" + JavaBoy.unsign(dmgcpu.ioHandler.registers[0x02]) + "    ");
						outStream.write(0);

						writeInt(outStream, -1);

						outStream.write(dmgcpu.ioHandler.getRegisters()[0x01]);
						outStream.flush();
						dmgcpu.ioHandler.getRegisters()[0x02] &= 0x7F;
						//      System.out.println("--> " + JavaBoy.unsign(dmgcpu.ioHandler.registers[0x01]));

						//     if ((clock & 0x01) != (JavaBoy.unsign(dmgcpu.ioHandler.registers[0x02]) & 0x01)) {
						dmgcpu.ioHandler.getRegisters()[0x01] = (byte) data;
						dmgcpu.triggerInterrupt(dmgcpu.INT_SER);
						//     }
					} else if (initial == 0) {
						dmgcpu.ioHandler.getRegisters()[0x02] &= 0x7F;
						dmgcpu.ioHandler.getRegisters()[0x01] = (byte) data;
						dmgcpu.triggerInterrupt(dmgcpu.INT_SER);
					}


				}   

			} catch (IOException e) {
				// Nothing!
			}

			clientConnected = false;
			new ModalDialog(parent, "Connection lost", "The connection with the other", "machine has been lost.");
		}



	}