sub sophienet_scanner {
	use IO::Socket::INET;
	my(@filestat, @files, $file, $filesize, $sophnetsock, $reply);

	my($start_sophienet_time)=[gettimeofday];
	&debug("sophienet: starting scan of directory \"$scandir/$file_id\"...");

	my($remhost, $remport, $blksize);

# CONFIGURATION
	# Remote host - Host that is running Sophie
	$remhost			 = "localhost";
	# Report port - Port number to use.
	$remport			 = "sophnet(4009)";
	# Block size
	$blksize			 = 5120;
# END CONFIGURATION

	eval { die $sophnetsock	 = IO::Socket::INET->new(PeerAddr	=> $remhost,
							PeerPort	=> $remport,
							Proto		=> 'tcp',
							Timeout		=> 900,
							Blocking	=> 1);  }; 

	if(!$sophnetsock) {
		&tempfail("sophienet: unable to create network socket \($!\)\n");
	}

	# Sophie-net requires one file at a time - dammit

	opendir(SCANDIR, "$scandir/$file_id");
	my(@files) = grep { !/^\./ } readdir(SCANDIR);
	close(SCANDIR);

	foreach $file (@files) {
		@filestat = stat("$scandir/$file_id/$file");

		$filesize = $filestat[7];

		if($filesize > 0) {

			&debug("sophienet: scanning $file/$filesize");

			syswrite($sophnetsock, "$file/$filesize\r\n", length("$file/$filesize\r\n"));
			sysread($sophnetsock, $reply, 256);

			if($reply =~ /OK/) {

				open(SOPHNET_SCANFILE, "$scandir/$file_id/$file") or eval {
					# Lets be good, close the socket and then tempfail
					shutdown($sophnetsock, 2);
					close($sophnetsock);
					&tempfail("sophienet: unable to read file to scan \($!\)");
				};

				my($br, $bw, $buffer);

				do {
					$br = sysread(SOPHNET_SCANFILE, $buffer, $blksize);
					$bw = syswrite($sophnetsock, $buffer, $br);
					if($br != $bw) {
						shutdown($sophnetsock, 2);
						close($sophnetsock);
						close(SOPHNET_SCANFILE);
						&tempfail("sophienet: file transfer error \($!\)");
					}
				} while ($br > 0);

				close(SOPHNET_SCANFILE);
				sysread($sophnetsock, $reply, 256);

				# This code looks familiar...

				if($reply == 0) {
					&debug("sophienet: $scandir/$file_id/$file is clean!\n");
                } elsif($reply =~ m/^1/) {
					# Virus found, find out what it is
					if ($reply =~ m/^1:.*$/) {
						($quarantine_description) = ($reply =~ m/^1:(.*)$/);
						$quarantine_description=~s/\0//g;
					}

					&debug("sophienet: There be a virus! ($quarantine_description)");
					($quarantine_event=$quarantine_description)=~s/\s/_/g;
					$quarantine_event="SOPHIE-NET:" . $quarantine_event;
					$description .= "\n---sophie-net results ---\n";
					$description .= "FILE/DIR INFECTED : $scandir/$file_id/$file\n";
					$description .= "VIRUS FOUND       : $quarantine_description\n";

                } else {
                	# This implies a corrupt set of IDE files or resource problems...
					# Again, lets be good, say goodbye, shutdown socket

					syswrite($sophnetsock, "QUIT\r\n", 6);
					sysread($sophnetsock, $reply, 256);
					shutdown($sophnetsock, 2);
					close($sophnetsock);

					&tempfail("unknown Sophos scanner problem");
				}
			} else {
				&debug("sophienet: Unexpected reply ($reply), possible bug, file not scanned\n");
			}
		} else {
			&debug("sophienet: skipping 0-sized file $file");
		}
	}

	# All done *whew*, say goodbye, shutdown, and close
	syswrite($sophnetsock, "QUIT\r\n", 6);
	sysread($sophnetsock, $reply, 256);
	shutdown($sophnetsock, 2);
	close($sophnetsock);

	my($stop_sophienet_time)=[gettimeofday];
	my($sophienet_time) = tv_interval ($start_sophienet_time, $stop_sophienet_time);
	&debug("sophienet: finished scan of dir \"$scandir/$file_id\" in $sophienet_time secs");

}

