<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/halo" 
                       height="229" width="438"
                       status="SOCKETS: 0" viewSourceURL="srcview/index.html">
    <fx:Script>
        <![CDATA[
            import flash.events.Event;
            import flash.events.ServerSocketConnectEvent;
            import flash.net.ServerSocket;
            import flash.net.Socket;
            
            private var serverSocket:ServerSocket;
            private var sockets:Array;
            
            [Bindable] private var numSockets:int = 0;
            
            private function startServer():void
            {
                sockets = new Array;
                serverSocket = new ServerSocket;
                serverSocket.addEventListener(Event.CONNECT, socketConnectHandler);
                serverSocket.bind( int(portInput.text), hostInput.text);
                serverSocket.listen();
                
                hostInput.enabled = false;
                portInput.enabled = false;
                msgInput.enabled = true;
            }
            
            private function socketConnectHandler(event:ServerSocketConnectEvent):void
            {
                var socket:Socket = event.socket;
                socket.addEventListener(Event.CLOSE, socketCloseHandler);
                sockets.push(socket);
                numSockets++;
                status = "SOCKETS: "+numSockets;
            }
            
            private function socketCloseHandler(event:Event):void
            {
                for(var i:int=0; i<sockets.length; i++)
                {
                    if(sockets[i] == event.target)
                    {
                        sockets.splice(i, 1);
                        numSockets--;
                        status = "SOCKETS: "+numSockets;
                    }
                }
            }
            
            private function sendData():void
            {
                for each(var socket:Socket in sockets)
                {
                    socket.writeUTFBytes(msgInput.text);
                    socket.flush();
                }
                msgInput.text = '';
            }
        ]]>
    </fx:Script>
    
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    
    <s:Button id="sendBtn" label="Send data" 
              click="sendData()"
              enabled="{msgInput.text.length != 0}"
              x="337" y="146"/>
    <s:TextInput id="msgInput" x="91" y="145" width="234" enabled="false"/>
    <s:Label x="32" y="150" text="Message:"/>
    <s:Label x="100" y="20" text="IP address:"/>
    <s:TextInput id="hostInput" x="171" y="15" text="127.0.0.1"/>
    <s:Label x="140" y="50" text="port:"/>
    <s:TextInput id="portInput" x="171" y="45" text="8888" restrict="0-9"/>
    <s:Button id="startBtn" x="215" y="76" label="Start Server" 
              click="startServer();startBtn.enabled=false"/>
    <s:Line left="0" right="0" y="110">
        <s:stroke>
            <s:SolidColorStroke color="0"/>
        </s:stroke>
    </s:Line>
</s:WindowedApplication>