public void createNewKey(String username, boolean client,ObjectInputStream in, ObjectOutputStream out) {
		try {
			System.out.println("Initiating the key agreement protocol ...");
			System.out.println("Generating a Diffie-Hellman KeyPair...");
			KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH");
			kpg.initialize(PARAMETER_SPEC);
			KeyPair keyPair = kpg.genKeyPair();

			Message syn;
			KeyAgreement ka = null;
			initializationVector = null;
			if (client) {
				System.out.println("Receiving the server's public key ...");
				syn = (Message) in.readObject();
				byte[] keyBytes = syn.getEncondedPublicKey();
				KeyFactory kf = KeyFactory.getInstance("DH");
				X509EncodedKeySpec x509Spec = new X509EncodedKeySpec(keyBytes);
				PublicKey serverPublicKey = kf.generatePublic(x509Spec);

				System.out.println("Sending my public key ...");
				keyBytes = keyPair.getPublic().getEncoded();
				syn.setEncondedPublicKey(keyBytes);
				out.writeObject(syn);
				out.reset();
				out.flush();
				System.out.println("Performing the KeyAgreement...");
				ka = KeyAgreement.getInstance("DH");
				ka.init(keyPair.getPrivate());
				ka.doPhase(serverPublicKey, true);

				System.out.println("Receiving the initialization vector ...");
				initializationVector = new byte[8];
				syn = (Message) in.readObject();
				initializationVector = syn.getInitializationVector();
			} else {
				syn=new Message(username, "NEW");
				System.out.println("Sending the server public key to client: "+ username);
				byte[] keyBytes = keyPair.getPublic().getEncoded();
				syn.setEncondedPublicKey(keyBytes);
				out.writeObject(syn);
				out.reset();
				out.flush();

				System.out.println("Receiving client's public key: " + username);
				syn = (Message) in.readObject();
				keyBytes = syn.getEncondedPublicKey();
				KeyFactory kf = KeyFactory.getInstance("DH");
				X509EncodedKeySpec x509Spec = new X509EncodedKeySpec(keyBytes);
				PublicKey clientPublicKey = kf.generatePublic(x509Spec);

				System.out.println("Performing the KeyAgreement...");
				ka = KeyAgreement.getInstance("DH");
				ka.init(keyPair.getPrivate());
				ka.doPhase(clientPublicKey, true);
				
				System.out.println("Create and send the IVParameterSpec to the client: "+ username);
				initializationVector = new byte[8];
				SecureRandom random = new SecureRandom();
				random.nextBytes(initializationVector);
				syn.setInitializationVector(initializationVector);
				out.writeObject(syn);
				out.reset();
				out.flush();
			}

			System.out.println("Creating a session key ...");
			byte[] sessionKeyBytes = ka.generateSecret();
			SecretKeyFactory skf = SecretKeyFactory.getInstance("TripleDES");
			DESedeKeySpec tripleDesSpec = new DESedeKeySpec(sessionKeyBytes);
			sessionKey = skf.generateSecret(tripleDesSpec);

			System.out.println("Creating the CipherStreams to be used with server...");

			Cipher decrypter = Cipher.getInstance("TripleDES/CFB8/NoPadding");
			Cipher encrypter = Cipher.getInstance("TripleDES/CFB8/NoPadding");

			IvParameterSpec spec = new IvParameterSpec(initializationVector);

			encrypter.init(Cipher.ENCRYPT_MODE, sessionKey, spec);
			decrypter.init(Cipher.DECRYPT_MODE, sessionKey, spec);

			cipherOut = new ObjectOutputStream(new CipherOutputStream(out,encrypter));
			cipherOut.flush();
			cipherIn = new ObjectInputStream(new CipherInputStream(in,	decrypter));

			if (!client){
				// add a new table entry for future connections
				SessionKey key = new SessionKey(initializationVector,sessionKey);
				record = new Record(username, key, System.currentTimeMillis());
			}

		} catch (Exception e) {
			if (DEBUG)
				e.printStackTrace();
		}

	}