public void SendMessages()
        {

            foreach (var message in Directory.GetFiles(Settings._instance.OutFolderPath,"*.txt"))
            {

                Queue queue = new Queue();
                try
                {
                    using (FileStream f = new FileStream(message, FileMode.Open, FileAccess.ReadWrite))
                    {
                        using (StreamReader newMsg = new StreamReader(f))
                        {
                            String msg = "";

                             msg = newMsg.ReadToEnd();
                            Message msgMessage = new Message(msg);

                            if (msgMessage.getElement("MSH", 8) == "ACK")
                            {
                                queue.Sent = true;
                                queue.Garbage = true;
                               // _repository.SaveChangesQueue(queue);
                               

                            }
                            else
                            {

                              //  _repository.CreateMhistory(msg);

                               // queue = _repository.CreateQueueRecord(msg);

                                if (Send(msg, queue))
                                {
                                    //queue = _repository.GetQueue(queue.ID);

                                    //queue.Sent = true;
                                    //_repository.SaveChangesQueue(queue);

                                }
                            }
                        }

                      
                    }
                    if (File.Exists(message))
                    {
                        if (queue.Sent == true)
                            File.Delete(message);
                    }
                }
                catch (Exception ex)
                {
                    ErrorHandler._ErrorHandler.LogError( ex, "- Problem sending message - "+ message);
                    
                }
                
            }

        }
        // This method is used to send a message to the server
        public  bool Send(string cmdstring, Queue queue)
        {
            cmdstring = HL7.CreateMLLPMessage(cmdstring);
            exceptionthrown = false;
            //var parameters = os_util.ParseParams(cmdstring);

            Message m = new Message(cmdstring);
            if (m.getElement("MSH", 8).ToUpper().ToString() == "ACK")
                return true;

            if (cmdstring.Length > 0)
            {
                try
                {
                    // We need a connection to the server to send a message
                    if (connectionsocket.Connected)
                    {

                        byte[] byData = System.Text.Encoding.ASCII.GetBytes(cmdstring);

                        try
                        {
                            connectionsocket.Send(byData);
                            queue.Sent = true;
                            _repository.SaveChangesQueue(queue);
                        }
                        catch (Exception ex)
                        {
                            ErrorHandler._ErrorHandler.LogError(ex, "Error sending", this);
                            connected = connectionsocket.Connected ? true : false ;
                            Connect(Settings._instance.RemoteIPAddress, Convert.ToInt32(Settings._instance.RemotePort));

                        }
                       
                        try
                        {

                           //ProcessSyncRecieve();
                           var item1 = socketpool.Pop();
                            item1.UserToken = new OSUserToken(this.connectionsocket,
                        Convert.ToInt32(Settings._instance.BufferSize), this._repository);

                           bool IOPending = connectionsocket.ReceiveAsync(item1);
                           // comment this for faster reads
                           if (!IOPending)
                           {
                               ProcessReceive(item);
                           }


                        }
                        catch (Exception ex)
                        {
                                
                              ErrorHandler._ErrorHandler.LogError(ex, "Did not recieve a message after 3 seconds");  
                        }
                        return true;
                    }
                    else
                    {
                        connected = false;
                        Connect(Settings._instance.RemoteIPAddress, Convert.ToInt32(Settings._instance.RemotePort));
                        return false;

                    }
                }
                catch (Exception ex)
                {
                    ErrorHandler._ErrorHandler.LogError(ex, "Error sending", this);
                    lasterror = ex.ToString();
                    return false;
                }
            }
            else
            {
                lasterror = "No message provided for Send.";
               ErrorHandler._ErrorHandler.LogInfo(lasterror);
                
                return false;
            }
        }
        public void myFileWatcher_ChangeDetecter(object sender,
        System.IO.FileSystemEventArgs e)
        {
            _timerClient.Stop();
            Queue queue = new Queue();
            // do something here.... as in read the file in and check it and then send it to the server 
            //if we are connected. if not connect and then send off
            try
            {
                using (StreamReader newMsg = new StreamReader(e.FullPath))
                {
                    String msg = "";

                    try
                    {
                        msg = newMsg.ReadToEnd();
                    }
                    catch (Exception ex)
                    {
                        ErrorHandler._ErrorHandler.LogError(ex, "Error opening file to send messages out bound");

                    }
                    _repository.CreateMhistory(msg);
                    queue = _repository.CreateQueueRecord(msg);

                    try
                    {
                        if (Send(msg, queue))
                        {
                            //queue.Sent = true;
                            // _repository.SaveChangesQueue(queue);

                        };
                    }
                    catch (Exception ex)
                    {
                        ErrorHandler._ErrorHandler.LogError(ex, "Error sending this message: " + msg);
                    }


                }

                if (queue.Sent == true)
                    File.Delete(e.FullPath);

            }
            catch (Exception ex)
            {
                ErrorHandler._ErrorHandler.LogError(ex, "ERROR in the file watcher");
                _timerClient.Start();
            }
            _timerClient.Start();
        }