private void StatusChangeMonitor()
        {
            uint rc;

              IntPtr hContext = IntPtr.Zero;

              _reader_state = SCARD.STATE_UNAWARE;
              _card_atr = null;

              rc =
            SCARD.EstablishContext(SCARD.SCOPE_SYSTEM, IntPtr.Zero,
                               IntPtr.Zero, ref hContext);
              if (rc != SCARD.S_SUCCESS)
            return;

              SCARD.READERSTATE[] states = new SCARD.READERSTATE[1];

              states[0] = new SCARD.READERSTATE();
              states[0].szReader = _reader_name;
              states[0].pvUserData = IntPtr.Zero;
              states[0].dwCurrentState = 0;
              states[0].dwEventState = 0;
              states[0].cbAtr = 0;
              states[0].rgbAtr = null;

              while (_status_change_running)
              {
            rc = SCARD.GetStatusChange(hContext, 250, states, 1);

            if (!_status_change_running)
              break;

            if (rc == SCARD.E_TIMEOUT)
              continue;

            if (rc != SCARD.S_SUCCESS)
            {
              _last_error = rc;

              SCARD.ReleaseContext(hContext);
              if (_status_change_callback != null)
            _status_change_callback(0, null);
              break;
            }

            states[0].dwCurrentState = states[0].dwEventState;

            if ((states[0].dwEventState & SCARD.STATE_CHANGED) != 0)
            {
              if (_status_change_callback != null)
              {
            CardBuffer card_atr = null;

            if ((states[0].dwEventState & SCARD.STATE_PRESENT) != 0)
              card_atr =
                new CardBuffer(states[0].rgbAtr, (int)states[0].cbAtr);

            _status_change_callback(states[0].dwEventState & ~SCARD.
                                    STATE_CHANGED, card_atr);
              }
            }
              }

              SCARD.ReleaseContext(hContext);
        }
        private void UpdateState()
        {
            uint rc;

              IntPtr hContext = IntPtr.Zero;

              _reader_state = SCARD.STATE_UNAWARE;
              _card_atr = null;

              rc =
            SCARD.EstablishContext(SCARD.SCOPE_SYSTEM, IntPtr.Zero,
                               IntPtr.Zero, ref hContext);
              if (rc != SCARD.S_SUCCESS)
              {
            _last_error = rc;
            return;
              }

              SCARD.READERSTATE[] states = new SCARD.READERSTATE[1];

              states[0] = new SCARD.READERSTATE();
              states[0].szReader = _reader_name;
              states[0].pvUserData = IntPtr.Zero;
              states[0].dwCurrentState = 0;
              states[0].dwEventState = 0;
              states[0].cbAtr = 0;
              states[0].rgbAtr = null;

              rc = SCARD.GetStatusChange(hContext, 0, states, 1);
              if (rc != SCARD.S_SUCCESS)
              {
            SCARD.ReleaseContext(hContext);
            return;
              }

              SCARD.ReleaseContext(hContext);

              _reader_state = states[0].dwEventState;

              if ((_reader_state & SCARD.STATE_PRESENT) != 0)
              {
            _card_atr = new CardBuffer(states[0].rgbAtr, (int)states[0].cbAtr);
              }
        }
        private void UpdateState()
        {
            uint rc;

              _reader_state = SCARD.STATE_UNAWARE;
              _card_atr = null;

              if (Connected)
              {
            byte[] atr_buffer = new byte[36];
            uint atr_length = 36;

            uint dummy = 0;

            rc =
              SCARD.Status(_hCard, IntPtr.Zero, ref dummy,
                       ref _reader_state, ref _active_protocol, atr_buffer,
                       ref atr_length);
            if (rc != SCARD.S_SUCCESS)
            {
              _last_error = rc;
              return;
            }

            _card_atr = new CardBuffer(atr_buffer, (int)atr_length);

              }
              else
              {
            SCARD.READERSTATE[] states = new SCARD.READERSTATE[1];

            states[0] = new SCARD.READERSTATE();
            states[0].szReader = _reader_name;
            states[0].pvUserData = IntPtr.Zero;
            states[0].dwCurrentState = 0;
            states[0].dwEventState = 0;
            states[0].cbAtr = 0;
            states[0].rgbAtr = null;

            rc = SCARD.GetStatusChange(_hContext, 0, states, 1);
            if (rc != SCARD.S_SUCCESS)
            {
              _last_error = rc;
              return;
            }

            _reader_state = states[0].dwEventState;

            if ((_reader_state & SCARD.STATE_PRESENT) != 0)
            {
              _card_atr = new CardBuffer(states[0].rgbAtr, (int)states[0].cbAtr);
            }
              }
        }