use IO::Socket;
use IO::Select;# Set the input terminator to a zero byte string, pursuant to the
# protocol in the flash documentation.
$/ = "\0";# Create a new socket, on port 7777
$lsn = new IO::Socket::INET(Listen => 1, 
LocalPort => 7777,
    Reuse => 1,
    Proto => 'tcp' )
   or die ("Couldn't start server: $!");# Create an IO::Select handler
$sel = new IO::Select( $lsn );# Close filehandlesclose(STDIN); close(STDOUT);warn "Server ready.  Waiting for connections . . . \n";# Enter into while loop, listening to the handles that are available.
while( @read_ready = $sel->can_read ) { foreach $fh (@read_ready ) { # Create a new socket
if($fh == $lsn) {
$new = $lsn->accept;
$sel->add($new);
push( @data, "SERVER: User (" . fileno($new) . ") has joined.");
warn "Connection from " . $new->peerhost . ".\n";
} # Handle connection
else { $input = <$fh>;

chomp $input;

if ( $input eq '') {
push( @data, fileno($fh) . " has left.");
warn "Disconnection from " . $new->peerhost . ".\n";
$sel->remove($fh);
$fh->close;
}
else {
$output = "(" . fileno($fh) . ") $input";
push( @data, $output );
}
}
} # Write to the clients that are available
foreach $fh ( @write_ready = $sel->can_write(0) ) {
foreach $line (@data) {
print $fh "$line\0";
}
}

undef @data;}warn "Server ended.\n";