@Override
	public boolean waitForDeviceReady(long timeout) throws ReaderException, UnsupportedDeviceException {

		try{
			this.connect();
		}
		catch(ReaderException e)
		{
			throw e;
		}
		
		if (this.terminal == null)
			throw new ReaderException("terminal is null");

		try {
			// wait timeout milisecond for card being present
			if (this.terminal.waitForCardPresent(timeout)) 
			{
				this.card = terminal.connect("*");
				
				byte[] historicalBytes = card.getATR().getHistoricalBytes();

				if(historicalBytes.length >= 11)
				{
					if (historicalBytes[9] == (byte)0xff && historicalBytes[10] == (byte)0x40) {
						this.connectionType = ConnectionType.Active;
						this.connection = new ActiveConnection(this,this.__debugMode);
					}
					else {
						this.connectionType = ConnectionType.Passive;
						this.connection = new PassiveConnection(this,(byte)0x08,this.__debugMode);
					}
				}
				else
				{
					this.connectionType = ConnectionType.Unknown;
					this.connection = null;
					throw new UnsupportedDeviceException("The device is neither active nor passive");
				}
				this.channel = card.getBasicChannel();
				if(__debugMode)
					logger.log(Level.INFO,"channel is open to the device : " + this.connectionType.toString() + " connection" );
				return true;
			} 
			else 
			{
				if(__debugMode)
					logger.log(Level.WARNING,"device is not present. channel could not be opened.");
				
				this.connectionType = null;
				this.connection = null;
				return false;
			}
		} 
		catch (CardException e) {
			this.connectionType = null;
			this.connection = null;
			throw new ReaderException("no device found");
		}
	}