public static Socket accept ( ServerSocket serverSocket , final boolean runInANewThread , @ ClosureParams ( value = SimpleType . class , options = "java.net.Socket" ) final Closure closure ) throws IOException { final Socket socket = serverSocket . accept ( ) ; if ( runInANewThread ) { new Thread ( new Runnable ( ) { public void run ( ) { invokeClosureWithSocket ( socket , closure ) ; } } ) . start ( ) ; } else { invokeClosureWithSocket ( socket , closure ) ; } return socket ; }
Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread or the calling thread as needed .