using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#if false

//namespace ERPSClient
//{
public class ERPSClient
	{
    string ipAddr;
    Int32 port;
    

		public ERPSClient()
		{
			//Client = null;
			ipAddr = string.Empty;
			port = 0;
		}
	
	public bool Connect(String ipAddr, Int32 port)
	{
			if (this.Client != null)
			{
				if (this.Client.Connected == true)
				{
					this.Disconnect();
				}
			}

			Client = new Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp);
			IAsyncResult result = Client.BeginConnect(ipAddr, port, nullptr, nullptr);
			bool success = result.AsyncWaitHandle.WaitOne(5000, true);
			if (!success)
			{
				return false;
			}
			Client.ReceiveTimeout = 0;
			this.ipAddr = ipAddr;
			this.port = port;

			return true;
	}

	public void Disconnect()
	{
			if (this.Client == null)
			{

			}
			else
			{
				try
				{
					this.ipAddr = "";
					this.port = 0;
					if (this.Client.Connected)
					{
						this.Client.Shutdown(SocketShutdown::Both);
					}
					this.Client.Close();
				}
				catch //(...)
			    {

				}
			}
	}

	public bool  IsConnected()
	{
		return this.Client.Connected;
	}

		public String  Query(String writeString)
		{
				//System::Text::Encoding enc = System::Text::Encoding::ASCII;
				var writeData = enc.GetBytes(writeString + "\n");
				var readData = new Byte[1024];

				if (this.Client.Connected)
				{
					Int32 bytesSent = this.Client.Send(writeData);
					Int32 bytesRec = this.Client.Receive(readData);
					return enc.GetString(readData, 0, bytesRec);
				}
				else
				{
					throw new InvalidOperationException("TCPIP Socket not connected");
				}
		}

		public void  Write(String writeString)
		{
				//System::Text::Encoding ^ enc = System::Text::Encoding::ASCII;

				if (this.Client.Connected)
				{
					var writeData = enc.GetBytes(writeString + "\n");
					Int32 bytesSent = this.Client.Send(writeData);
				}
				else
				{
					throw new InvalidOperationException("Can not perform TCPIP Write because no connection exists.");
				}
		}

		public void  WriteBinary(Byte[] dataToWrite)
		{
				if (this.Client.Connected)
				{
					Int32 sTimeout = this.Client.SendTimeout;
					this.Client.SendTimeout = 0;
					UInt32 bytesSent = (UInt32)(this.Client.Send(dataToWrite));
					this.Client.SendTimeout = sTimeout;
				}
				else
				{
					throw new InvalidOperationException("Can not perform TCPIP WriteBinary because no connection exists.");
				}
		}

		public Byte[]  ReadBinary(Int32 numBytes)
		{
				Byte[] readData = new Byte[numBytes];
				Int32 bytesRec = 0;
				Int32 bytesLeft = numBytes;

				if (this.Client.Connected)
				{
					do
					{
						bytesRec += this.client.Receive(readData, bytesRec, bytesLeft - bytesRec, SocketFlags::None);
					} while (bytesRec < numBytes);
				}
				else
				{
					throw new InvalidOperationException("TCPIP Socket not connected");
				}

				return readData;
		}

		public String  Read()
		{
				if (this.Client.Connected)
				{
					//System::Text::Encoding enc = System::Text::Encoding::ASCII;
					var writeData = new Byte[1024];
					Int32 bytesRec = this.Client.Receive(readData);
					return enc.GetString(readData, 0, bytesRec);
				}
				else
				{
					throw new InvalidOperationException("Can not perform TCPIP read because no connection exists.");
				}
		}
}
#endif