         this.r2c_schedule = [];
     }
 
+    /* Return a function that shows an error message and closes the other half
+       of a communication pair. */
+    private function socket_error(message:String, other:Socket):Function
+    {
+        return function(e:Event):void {
+            if (e is TextEvent)
+                log(message + ": " + (e as TextEvent).text + ".");
+            else
+                log(message + ".");
+            if (other && other.connected)
+                other.close();
+            dispatchEvent(new Event(Event.COMPLETE));
+        };
+    }
+
     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(Event.CLOSE, socket_error("Relay: closed", s_c));
+        s_r.addEventListener(IOErrorEvent.IO_ERROR, socket_error("Relay: I/O error", s_c));
+        s_r.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socket_error("Relay: security error", s_c));
         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, function (e:Event):void {
            log("Client: closed.");
            if (s_r && s_r.connected)
                s_r.close();
            dispatchEvent(new Event(Event.COMPLETE));
        });
        s_c.addEventListener(IOErrorEvent.IO_ERROR, function (e:IOErrorEvent):void {
            log("Client: I/O error: " + e.text + ".");
            if (s_r && s_r.connected)
                s_r.close();
            dispatchEvent(new Event(Event.COMPLETE));
        });
        s_c.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function (e:SecurityErrorEvent):void {
            log("Client: security error: " + e.text + ".");
            if (s_r && s_r.connected)
                s_r.close();
            dispatchEvent(new Event(Event.COMPLETE));
        });
        s_c.addEventListener(ProgressEvent.SOCKET_DATA, client_to_relay);

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

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;
