 import flash.events.IOErrorEvent;
 import flash.events.ProgressEvent;
 import flash.events.SecurityErrorEvent;
+import flash.events.TextEvent;
 import flash.net.Socket;
 import flash.utils.ByteArray;
 import flash.utils.clearTimeout;

        this.r2c_schedule = [];
    }

    public function connect():void
    {
        s_r = new Socket();

        s_r.addEventListener(Event.CONNECT, relay_connected);
        s_r.addEventListener(Event.CLOSE, function (e:Event):void {
            log("Relay: closed.");
            if (s_c && s_c.connected)
                s_c.close();
            dispatchEvent(new Event(Event.COMPLETE));
        });
        s_r.addEventListener(IOErrorEvent.IO_ERROR, function (e:IOErrorEvent):void {
            log("Relay: I/O error: " + e.text + ".");
            if (s_c && s_c.connected)
                s_c.close();
            dispatchEvent(new Event(Event.COMPLETE));
        });
        s_r.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function (e:SecurityErrorEvent):void {
            log("Relay: security error: " + e.text + ".");
            if (s_c && s_c.connected)
                s_c.close();
            dispatchEvent(new Event(Event.COMPLETE));
        });
        s_r.addEventListener(ProgressEvent.SOCKET_DATA, relay_to_client);

        log("Relay: connecting to " + addr_r.host + ":" + addr_r.port + ".");

        s_c = new Socket();

        s_c.addEventListener(Event.CONNECT, client_connected);
        s_c.addEventListener(Event.CLOSE, socket_error("Client: closed", s_r));
        s_c.addEventListener(IOErrorEvent.IO_ERROR, socket_error("Client: I/O error", s_r));
        s_c.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socket_error("Client: security error", s_r));
        s_c.addEventListener(ProgressEvent.SOCKET_DATA, client_to_relay);

        log("Client: connecting to " + addr_c.host + ":" + addr_c.port + ".");
