import java.net.*;
import java.io.*;

public class ClientTester {

public static void main(String[] args) {
    int port;
    try {
      port = Integer.parseInt(args[0]);
    }
    catch (Exception e) {
      port = 0;
    }
    try {
      ServerSocket server = new ServerSocket(port, 1);
      System.out.println("Listening for connections on port "
        + server.getLocalPort());
      while (true) {
        Socket connection = server.accept();
        try {
          System.out.println("Connection established with "
           + connection);
          Thread input = new InputThread(connection.getInputStream());
          input.start();
          Thread output
            = new OutputThread(connection.getOutputStream());
          output.start();
// wait for output and input to finish
          try {
            input.join();
            output.join();
          }
          catch (InterruptedException e) {
          }
        }
        catch (IOException e) {
          System.err.println(e);
        }
        finally {
          try {
          if (connection != null) connection.close();
          }
          catch (IOException e) {}
        }
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}

class InputThread extends Thread {
  InputStream in;

  public InputThread(InputStream in) {
    this.in = in;
  }

  public void run() {
    try {
      while (true) {
        int i = in.read();
        if (i == -1) break;
        System.out.write(i);
      }
    }
    catch (SocketException e) {
// output thread closed the socket
    }
    catch (IOException e) {
      System.err.println(e);
    }
    try {
      in.close();
    }
    catch (IOException e) {
    }
  }
}

class OutputThread extends Thread {
  Writer out;

  public OutputThread(OutputStream out) {
    this.out = new OutputStreamWriter(out);
  }

  public void run() {
    String line;
    BufferedReader in
      = new SafeBufferedReader(new InputStreamReader(System.in));
    try {
      while (true) {
        line = in.readLine();
        if (line.equals(".")) break;
        out.write(line +"\r\n");
        out.flush();
      }
    }
    catch (IOException e) {
    }
    try {
      out.close();
    }
    catch (IOException e) {
    }
  }
}

class SafeBufferedReader extends BufferedReader {

  public SafeBufferedReader(Reader in) {
    this(in, 1024);
  }

  public SafeBufferedReader(Reader in, int bufferSize) {
    super(in, bufferSize);
  }
  private boolean lookingForLineFeed = false;

  public String readLine() throws IOException {
    StringBuffer sb = new StringBuffer("");
    while (true) {
      int c = this.read();
      if (c == -1) { // end of stream
      return null;
      }
      else if (c == '\n') {
        if (lookingForLineFeed) {
          lookingForLineFeed = false;
          continue;
        }
        else {
          return sb.toString();
        }
      }
      else if (c == '\r') {
        lookingForLineFeed = true;
        return sb.toString();
      }
      else {
        lookingForLineFeed = false;
        sb.append((char) c);
      }
    }
  }
}