// package pic20b;

import java.io.*;
import java.net.*;


public class Server2 {

    public static void main(String[] args) throws IOException {

	final int qLen = 6;
	final int port = 4444;
	Socket sock;
	ServerSocket servsock = new ServerSocket(port, qLen);

	while (true) {
	    sock = servsock.accept();
	    new RequestHandler(sock).start();
	}

    }


    static class RequestHandler extends java.lang.Thread {
	String query = "How are you?";
	String reply;
	Socket sock;

	RequestHandler(Socket s) {
	    sock = s;
	}

	public void run() {
	    System.out.println("Thread running: " + currentThread());

	    try {
		PrintStream sout = new PrintStream(sock.getOutputStream());
		BufferedReader sin = new BufferedReader(
			new InputStreamReader(sock.getInputStream()));
		sout.println(query);
		sout.flush();
		reply = sin.readLine();

		if (reply.indexOf("thank") > -1) {
		    System.out.println("Nice Person");
		} else {
		    System.out.println("Probably an AI experiment");
		}
		sock.close();
	    } catch (IOException ioe) {
		System.err.println(ioe);
	    }

	}

    }


}
