import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by jackrosenhauer on 5/5/15.
 */
public class Main {

    public static void main(String[] args){
        final int DEFAULT_PORT = 8080; // For security reasons, only root can use ports < 1024.
        int portNumber = args.length > 1 ? Integer.parseInt(args[0]) : DEFAULT_PORT;
        ServerSocket serverSocket;
        int threadsCreated = 0;
        try{
            serverSocket = new ServerSocket(portNumber);


            while (true) {
                Thread t = new Worker(serverSocket);
                t.start();
                threadsCreated++;
                System.out.println("Threads Created & Processed: " + threadsCreated);
            }


        } catch (IOException e) {
            System.err.println("Oops!  " + e);
        } catch (Exception e){
            System.out.println("Something else went terribly wrong");
            e.printStackTrace();
        }

    }
}
