public TwsRtdServerWrapper(TwsRtdServer server, TwsRtdServerConnection connection)
 {
     m_server     = server;
     m_connection = connection;
 }
        private IDictionary <string, int> m_mktDataRequestsStrToIdMapping       = new Dictionary <string, int>();                     // map: mktDataRequestStr(e.g. "IBM_STK_...") => twsReqId

        // constructor
        public TwsRtdServerConnection(TwsRtdServer twsRtdServer, string connectionStr)
        {
            try
            {
                SetError(TwsRtdServerErrors.NO_ERROR);

                m_twsReqIdNext = 0;

                // parse connection string to host/port
                ExtractConnectionParams(connectionStr);

                // check for error
                if (GetErrorCode() != TwsRtdServerErrors.NO_ERROR)
                {
                    return;
                }

                m_twsRtdServerWrapper = new TwsRtdServerWrapper(twsRtdServer, this);

                this.m_eClientSocket = new EClientSocket(m_twsRtdServerWrapper, m_eReaderSignal);

                // allow PACEAPI feature
                this.m_eClientSocket.SetConnectOptions("+PACEAPI");

                // connect to TWS
                this.m_eClientSocket.eConnect(m_host, m_port, m_clientId, false);

                if (m_eClientSocket.ServerVersion == 0)
                {
                    SetError(TwsRtdServerErrors.CANNOT_CONNECT_TO_TWS); // TwsRtdServer: error connecting to TWS
                    return;
                }

                var reader = new EReader(m_eClientSocket, m_eReaderSignal);

                reader.Start();

                new Thread(() =>
                {
                    while (m_eClientSocket.IsConnected())
                    {
                        m_eReaderSignal.waitForSignal();
                        reader.processMsgs();
                    }
                })
                {
                    IsBackground = true
                }.Start();

                // wait 5 seconds till fully connected
                int i = 0;
                while (m_twsRtdServerWrapper.NextOrderId <= 0 && i < 50)
                {
                    Thread.Sleep(100);
                    i++;
                }
                if (i >= 50)
                {
                    SetError(TwsRtdServerErrors.CANNOT_CONNECT_TO_TWS); // TwsRtdServer: error connecting to TWS
                }

                // send request market data type
                this.m_eClientSocket.reqMarketDataType(2);
                this.m_eClientSocket.reqMarketDataType(4);
            }
            catch
            {
                SetError(TwsRtdServerErrors.CANNOT_CONNECT_TO_TWS); // TwsRtdServer: error connecting to TWS
            }
        }