#!/usr/local/bin/perl
require 'sys/socket.ph';
$sockaddr = 'S n a4 x8';
chop ($hostname = `hostname`);
($name, $aliases, $proto) = getprotobyname ('tcp');
$port = 9393; 
$thisport = pack($sockaddr, &AF_INET, $port, "\0\0\0\0");

socket (S, &PF_INET, &SOCK_STREAM, $proto) ||
    die "cannot create socket\n";
bind (S,$thisport) || die "cannot bind socket\n";
listen (S,5) || die "cannot listen socket\n";
for (;;)  {
	accept (NS,S) || die "cannot accept socket\n";
	print NS &fortune;
	close NS;
}

sub fortune {
  @fortunes = split (/\n%%\n/, <<'END') unless @fortunes;
A fool and his money are soon parted.
%%
a penny saved is a penny earned.
%%
i hope this works.
%%
END
  splice(@fortunes,int(rand(@fortunes)),1)."\n";
}
