using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
// Reference from https://www.c-sharpcorner.com/article/socket-programming-in-C-Sharp/
namespace myshutdown
{
	class Program
	{
		public static void Main(string[] args)
		{			
			/*
			System.Diagnostics.Process.Start("shutdown","/s /t 180");
			// DOS> shutdown /a
			*/
			StartServer();
		}
		public static void StartServer()  
    	{   
			string HostName = Dns.GetHostName();  
			IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);
			Console.WriteLine(HostName);			
			IPHostEntry host;
			IPAddress ipAddress = null;
            foreach(IPAddress ip in ipaddress)  
            {  
                Console.WriteLine(ip.ToString());
            	host = Dns.GetHostEntry(ip); // localhost
				ipAddress = ip;            	
            }         	
			// use only last ipaddress in server            
        	IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11111);
        	try {   
            	Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);    
	            listener.Bind(localEndPoint); 
    	        listener.Listen(10);  
    	        Console.WriteLine("Waiting for a connection...");  
    	        Socket handler = listener.Accept();  
    	    }  
    	    catch (Exception e)  
    	    {  
    	        Console.WriteLine(e.ToString());  
    	    }
			Process cmd = new Process();
			cmd.StartInfo.FileName = @"cmd.exe";
			cmd.StartInfo.Arguments = @"/K shutdown /s /t 180"; // dir  
			cmd.Start();
			cmd.WaitForExit();			    	    
    	    Console.WriteLine("\n to stop process by DOS> shutdown /a");  
    	    Console.ReadKey();  
    	}        	
	}
}