--- wadf-httpd.orig	2010-01-14 08:07:02.000000000 +0000
+++ wadf-httpd	2010-01-14 09:00:11.000000000 +0000
@@ -98,7 +98,7 @@
 
 function start($show_instance_info=null)
 {
-	global $prog, $ret, $out, $httpd, $conffile, $pidfile, $portfile, $lockfile, $errlogfile, $vhost_dir, $source_conffile, $WADF_HOME, $default_vhost_file, $default_error_page;
+	global $prog, $ret, $out, $httpd, $conffile, $pidfile, $portfile, $lockfile, $errlogfile, $vhost_dir, $source_conffile, $WADF_HOME, $default_vhost_file, $default_error_page, $existing_port;
 	if (file_exists($pidfile)) {
 		if (is_really_running(trim(file_get_contents($pidfile)))) {
 			print "$prog is already running\n";
@@ -142,14 +142,19 @@
 		// Find a good port to listen on
 		$sock = false;
 		$port = 10080;
-		while ($sock === false) {
-			$sock = @socket_create_listen($port);
-			if ($sock === false) {
-				$port++;
-			} else {
-				socket_shutdown($sock, 2);
-				socket_close($sock);
+		if (isset($existing_port) && is_numeric($existing_port)) {
+			$port = $existing_port;
+		} else {
+			while ($sock === false) {
+				$sock = @socket_create_listen($port);
+				if ($sock === false) {
+					$port++;
+				} else {
+					socket_shutdown($sock, 2);
+					socket_close($sock);
+				}
 			}
+
 		}
 		$fp = fopen($portfile, 'w');
 		fputs($fp, $port);
@@ -168,13 +173,17 @@
 
 function stop()
 {
-	global $httpd, $prog, $ret, $out, $pidfile, $conffile;
+	global $httpd, $prog, $ret, $out, $pidfile, $conffile, $portfile, $existing_port;
 	if (!file_exists($pidfile)) {
 		print "$prog is not running\n";
 		exit(0);
 	}
 	$pid = file_get_contents(trim($pidfile));
 	if (is_really_running($pid)) {
+		$port = trim(file_get_contents($portfile));
+		if (!empty($port)) {
+			$existing_port = $port;
+		}
 		echo "Stopping $prog: ";
 		exec("LANG=C $httpd -c 'PidFile $pidfile' -k graceful-stop", $out, $ret);
 		//exec("kill $pid", $out, $ret);
@@ -188,7 +197,7 @@
 
 function reload($show_instance_info=null)
 {
-	global $prog, $ret, $out, $conffile, $httpd, $pidfile;
+	global $prog, $ret, $out, $conffile, $httpd, $pidfile, $existing_port;
 	echo "Reloading $prog: ";
 	check_config();
 	if ($ret !== 0) {
