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

public class SocketServer3 {

//*********************************************************
    public static void main( String a[] ) {
	int thisPort = 5001;
	ServerSocket server;
	Socket connection;
		
	// start HouseDaemon to monitor SocketDaemons
	HouseDaemon3 snitch = new HouseDaemon3();
	
	try{
	    // open socket on port
	    server = new ServerSocket( thisPort );
	    System.out.println( "server waiting for connection..." );
	    
	    while( true ) {
		
		// wait for connections and spawn SocketDaemons to manage them
		connection = server.accept();
		SocketDaemon3 freshDaemon = new SocketDaemon3( snitch, connection );
		boolean response = snitch.watch( freshDaemon );
		while ( !response ) {
		    System.out.println( "HouseDaemon fails to respond" );
		    snitch = new HouseDaemon3();
		    response = snitch.watch( freshDaemon );
		}
		freshDaemon.start();
	    }
        } catch( IOException ioe ) {
	    System.out.println("server has no connection");
        }
	
    }

//*********************************************************
}
