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

public class ConnectionServer {
	private ServerSocket sockets = null;
	
	public ConnectionServer( int port ) throws IOException {
		sockets = new ServerSocket( port );
	}
	
	public Connection newConnection() throws IOException, SecurityException {
		if (sockets != null) {
			return new Connection( sockets.accept() );
		} else {
			throw new IOException( "Connection Server has not been properly initialized." );
		}
	}
	
	public void close() {
		if (!(sockets == null || sockets.isClosed())) {
			try {
				sockets.close();
			} catch (IOException e) {
				// ??! Not so sure what kind of I/O error (that I care about) could occur trying to close a
				//  ServerSocket... Either way, do nothing for now (until someone can enlighten me).
			} finally {
				sockets = null;
			}
		}
	}
}
