using System;
using System.Net;
using System.Threading;
using System.Net.Sockets;

using WMFix.Handlers;

namespace WMFix
{
    class Program
    {
        static Socket WM9110Socket;
        static Socket WM9115Socket;

        static void Main(String[] Args)
        {
            IPAddress BindIP = IPAddress.Parse(Settings.Bind_IP);
            IPEndPoint WM9110EndPoint = new IPEndPoint(BindIP, Settings.Bind_Port9110);
            IPEndPoint WM9115EndPoint = new IPEndPoint(BindIP, Settings.Bind_Port9115);

            WM9110Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            WM9110Socket.Bind(WM9110EndPoint);
            WM9110Socket.Listen(50000);
            WM9110Socket.BeginAccept(new AsyncCallback(WM9110Accept), null);

            WM9115Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            WM9115Socket.Bind(WM9115EndPoint);
            WM9115Socket.Listen(50000);
            WM9115Socket.BeginAccept(new AsyncCallback(WM9115Accept), null);

            Console.ReadLine();
        }

        private static void WM9115Accept(IAsyncResult Result)
        {
            try
            {
                WM9115Socket.BeginAccept(new AsyncCallback(WM9115Accept), null);

                Socket ClientSocket = WM9115Socket.EndAccept(Result);

                Console.WriteLine("9115: " + ClientSocket.RemoteEndPoint.ToString().Split(':')[0]);

                new Thread(() => { WM9115ClientHandle Handle = new WM9115ClientHandle(ClientSocket); }).Start();
            }
            catch { }
        }

        private static void WM9110Accept(IAsyncResult Result)
        {
            try
            {
                WM9110Socket.BeginAccept(new AsyncCallback(WM9110Accept), null);

                Socket ClientSocket = WM9110Socket.EndAccept(Result);

                Console.WriteLine("9110: " + ClientSocket.RemoteEndPoint.ToString().Split(':')[0]);

                new Thread(() => { WM9110ClientHandle Handle = new WM9110ClientHandle(ClientSocket); }).Start();
            }
            catch { }
        }
    }

    internal class WM9115ClientHandle
    {
        private Socket clientSocket;

        public WM9115ClientHandle(Socket clientSocket)
        {
            this.clientSocket = clientSocket;
        }
    }
}
