static void commandHandler(Socket socket) { var ctx = new Context(); if (socket == null) { ctx.InitPipeServer(Fd.FromPosixFd(0), Fd.FromPosixFd(1)); } else { ctx.InitSocketServer(socket, SocketServerFlags.Accepted); } ctx.RegisterCommand("FOO", foo); ctx.RegisterCommand("BAR", bar); ctx.RegisterCommand("INPUT", null); ctx.RegisterCommand("OUTPUT", null); for (; ;) { try { ctx.Accept(); } catch (ErrorException ex) when(ex.Error.Code == ErrorCode.EOF) { break; } try { ctx.Process(); } catch (Exception ex) { Console.Error.WriteLine("Processing failed: {}", ex.Message); continue; } } }