public bool AddSinkNode(SinkNode sinkNode)
 {
     bool result = false;
     try
     {
         var node = _db.GetAll<SinkNode>().FirstOrDefault(x => x.IPAddress == sinkNode.IPAddress && x.Port==sinkNode.Port);
         if (node != null)
         {
             throw new Exception("This Sink Node With this IP Address and Port already Exist");
         }
         else
         {
             result = _db.Add(sinkNode);
         }
         return result;
     }
     catch (Exception ex)
     {
         _db.Rollback();
         throw ex;
     }
 }
 public bool Edit(SinkNode model)
 {
     try
       {
     bool result = false;
      var sinkNode = _db.GetAll<SinkNode>().FirstOrDefault(x => x.IPAddress == model.IPAddress);
       if (sinkNode != null)
       {
           sinkNode.Name = model.Name;
           sinkNode.HostName = model.HostName;
           sinkNode.IPAddress = model.IPAddress;
           sinkNode.Port = model.Port;
           sinkNode.IsActive = model.IsActive;
         result= _db.Update(sinkNode);
       }
       return result;
       }
       catch (Exception ex)
       {
      _db.Rollback();
      throw ex;
       }
 }
 public void Update(SinkNode sinkNode)
 {
     if(sinkNode!=null)
        {
        _db.Update(sinkNode);
        }
 }
        private Iso8583Message ToFEP(Iso8583Message msgToSend, SinkNode sinkNode, out bool needReversal)
        {
            Message response = null;
               string responseMsg = string.Empty;
               needReversal = false;
               try
               {
               if (msgToSend == null)
               {
                   Logger.Log("Iso message is null.");
                   return SetReponseMessage(msgToSend, "20");  //Invalid response
               }

               if (sinkNode == null)
               {
                   Logger.Log("Sink node is null.");
                   return SetReponseMessage(msgToSend, "91");  //Issuer inoperative
               }

               int maxNoRetries = 3;
               int serverTimeout = 60000;

               sinkNode.IsActive = true;
               ClientPeer _clientPeer = new ClientPeer(sinkNode.Name, new TwoBytesNboHeaderChannel(
                       new Iso8583Ascii1987BinaryBitmapMessageFormatter(), sinkNode.IPAddress,
                       Convert.ToInt16(sinkNode.Port)), new Trx.Messaging.BasicMessagesIdentifier(11));
               _clientPeer.Connect();
               Thread.Sleep(1800);
               int retries = 0;
               while (retries < maxNoRetries)
               {
                   if (_clientPeer.IsConnected)
                   {
                       break;
                   }
                   else
                   {
                       _clientPeer.Close();
                       retries++;
                       _clientPeer.Connect();
                   }
                   Thread.Sleep(2000);
               }

               PeerRequest request = null;
               if (_clientPeer.IsConnected)
               {
                   request = new PeerRequest(_clientPeer, msgToSend);
                   request.Send();
                   request.WaitResponse(serverTimeout);
                   //request.MarkAsExpired();   //uncomment to test timeout

                   if (request.Expired)
                   {
                       Logger.Log("Connection timeout.");
                       needReversal = true;
                       return SetReponseMessage(msgToSend, "68");  //Response received too late
                   }
                   if (request != null)
                   {
                       response = request.ResponseMessage;
                       //ResponseMessage = GetResponseMesage(response as Iso8583Message);
                   }
                   return response as Iso8583Message;
               }
               else
               {
                   Console.WriteLine("\n Could not connect to the Sink Node..");
                   Console.BackgroundColor = ConsoleColor.Red;
                   Logger.Log("\n Could not connect to the Sink Node.");
                   return SetReponseMessage(msgToSend, "91");
               }
               }
               catch (Exception ex)
               {
               Logger.Log("ERROR: " + ex.Message);
               return SetReponseMessage(msgToSend, "06");  //Error
               }
        }