public ClientP2P() {

		/* Initialise GUI */
		gui = new ClientP2PGUI(this);

		/* Get server IP */
		server_IP = JOptionPane.showInputDialog("Enter server IP:");
		if (server_IP == null) System.exit(-1);
		else if (server_IP.equals("")) server_IP = "localhost";

		/* Generate random key */
		key = (int) (Math.random() * 255);

		/* (Re)build list of files in directory */
		try {
			File directory = new File(folder);
			if (!directory.isDirectory()) {
				gui.update("[Client] ERROR: Not a directory.");
				Thread.sleep(2000);
				System.exit(-1);
			}
		} catch (Exception e) {
			gui.update("[Client] Error: Bad directory.");
			System.exit(-1);
		}

		try {
			/* Open up streams */
			localSocket = new Socket(server_IP, server_port);
			inFromServer = new DataInputStream(localSocket.getInputStream());
			outToServer = new DataOutputStream(localSocket.getOutputStream());

			/* Confirm connection and set nickname */
			user_nickname = gui.question("Please enter nickname:");
			int code;
			while ((code = inFromServer.readInt()) == 1 || code == 3) {
				if (code == 3)
					user_nickname = gui.question("Server nickname rejected; try again.");
				outToServer.writeUTF(user_nickname);
			}
		} catch (Exception e) {
			gui.update("[Client] Error experienced while connecting with server.");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e1) {
			}
			System.exit(-1);
		}

		/* Start download listener thread */
		new Thread(new DownloadListener()).start();
		
		/* Start search query thread */
		new Thread(new SearchHandler()).start();

		/* Start server listener thread */
		new Thread(new Listener()).start();
	}