#!/usr/bin/php -q
<?php
include("/system/init.php");

// Set time limit to indefinite execution 
set_time_limit (0); 

// Set the ip and port we will listen on 
$address '0.0.0.0'
$port 1983;
$max_clients 5

// Array that will hold client information 
$clients = Array();
$banned = Array();

// Create a TCP Stream socket 
$sock socket_create(AF_INETSOCK_STREAM0); 
// Bind the socket to an address/port 

if (!@socket_setopt($sock,SOL_SOCKET,SO_REUSEADDR,1)) {
               echo 
"socket_setopt() failed: reason: ".socket_strerror(socket_last_error($sock))."\n";
               exit;
       }

socket_bind($sock$address$port) or die('Could not bind to address'); 
// Start listening for connections 
socket_listen($sock); 

// Loop continuously 
while ($quit != true) { 
    
// Setup clients listen socket for reading 
    
$read[0] = $sock
    for (
$i 0$i $max_clients$i++) 
    { 
        if (
$client[$i]['sock']  != null
            
$read[$i 1] = $client[$i]['sock'] ; 
    } 
    
// Set up a blocking call to socket_select() 
    
$ready socket_select($read,$write null,$except null,null); 
    
/* if a new connection is being made add it to the client array */ 
    
if (in_array($sock$read)) { 
        for (
$i 0$i $max_clients$i++) 
        { 
            if (
$client[$i]['sock'] == null) { 
                
srand ((double) microtime() * 1000000);
                
socket_getpeername($sock,&$ip);
                echo(
$ip);
                
                
$client[$i]['sock'] = socket_accept($sock);
                
$client[$i]["status"] = 'loggedout';
                
$client[$i]["ip"] = $ip;
                
$num $client[$i]["num"];
                
                if(
in_array($client[$i]["ip"],$banned)) {
                    print 
"killed ".$client[$i]["ip"];
                    
socket_close($client[$i]['sock']);
                    break;
                }
                
                
// greeting

                // challenge / authentication code
                // socket_write($client[$i]['sock'],'bla bla');

                
break; 
            } 
            elseif (
$i == $max_clients 1
                print (
"too many clients");
        } 
        if (--
$ready <= 0
            continue; 
    } 
// end if in_array 
     
    // If a client is trying to write - handle it now 
    
for ($i 0$i $max_clients$i++) // for each client 
    

        if (
in_array($client[$i]['sock'] , $read)) 
        { 
            
$input socket_read($client[$i]['sock'] , 1024); 
            if (
$input == null) { 
                
// Zero length string meaning disconnected 
                
unset($client[$i]); 
            } 
            
$client[$i]['data'] .= ereg_replace("[\t\n\r]","",$input);
            if(
ereg("\n",$input)) {
                
$data explode(" ",$client[$i]['data']);
                
$data[0] = strtolower($data[0]);

                
// if client is authenticated, do something

                // otherwise do something else


                // wipe data for next iteration to be sure
                
$client[$i]['data'] = '';
            }
        } else { 
            unset(
$client[$i]); 
        } 
    } 
// end while 
// Close the master sockets 

for ($i 0$i $max_clients$i++) { 
    
socket_close($client[$i]['sock']); 
}

socket_close($sock); 
?>