protected void onChannelOpen(Message msg) throws IOException {
        String type = msg.readString();
        int remoteid = (int) msg.readInt();

        // #ifdef DEBUG
    	if(log.isDebugEnabled())
    		log.debug("Open channel '" + type + "' [" + remoteid + "]");
        // #endif
        
        int remotepacket = (int) msg.readInt();
        int remotewindow = (int) msg.readInt();

        byte[] data = null;
        
        if(msg.available() > 0) { 
        	data = new byte[msg.available()];
        	msg.read(data);
        }

        if (factory == null) {
            sendChannelOpenFailure(remoteid, "This connection does not support the opening of channels");
            return;
        }

        Channel channel = null;
        try {
        	channel = factory.createChannel(this, type);
	        if (channel == null) {
	            sendChannelOpenFailure(remoteid, "Failed to create channel of type " + type);
	            return;
	        }  	
        }
        catch(ChannelOpenException coe) {
            sendChannelOpenFailure(remoteid, coe.getMessage() == null ? ( "Failed to create channel of type " + type + ". Reason " + coe.getReason()) : coe.getMessage() );
            return;        	
        }

        channel.init(this, remoteid, remotepacket, remotewindow);

        if (allocateChannel(channel) == -1) {
            sendChannelOpenFailure(remoteid, "Too many channels already open");

        } else {
        	try {
        		data = channel.open(data);
                sendChannelOpenConfirmation(channel, data);
        	}
        	catch(ChannelOpenException coe) {
                sendChannelOpenFailure(remoteid, coe.getMessage() == null ? ( "Failed to open channel. Reason " + coe.getReason()) : coe.getMessage() );
                return;        	
        	}
        }

        channel.fireChannelOpen(data);

    }