Index: tolsh/tolsh.vfs/lib/app-tolsh/tolsh.tcl
===================================================================
--- tolsh/tolsh.vfs/lib/app-tolsh/tolsh.tcl	(revision 76a56bf6d6f65d8aafe514a40706a45b98e5f183)
+++ tolsh/tolsh.vfs/lib/app-tolsh/tolsh.tcl	(revision a168f52b60243d0c4c4cbf84a4036c8fb5c2689e)
@@ -166,8 +166,15 @@
         set options(runmode) "slave"
       } elseif {$opt eq "shared"} {
+        # the next argument is the port number
+        set options(shared,port) [lindex $cmdline [expr {$i+1}]]
+        incr i
         if {$options(runmode) ne "" && $options(runmode) ne "shared"} {
           puts "Replacing previous run mode '$options(runmode)' with 'shared'"
         }
         set options(runmode) "shared"
+      }  elseif {$opt eq "shport"} {
+        # the next argument is the port number
+        set options(shared,port) [lindex $cmdline [expr {$i+1}]]
+        incr i
       } else {
         append delayed_msg "\nignoring unknown option: '$item'"
@@ -444,5 +451,5 @@
   } elseif {$options(runmode) eq "shared"} {
     puts "runmode=shared"
-    run_as_shared    
+    run_as_shared $tolcomm_dir
   
   } elseif {$options(runmode) eq "slave"} {
@@ -456,6 +463,7 @@
   package require tolserver
 
-  clone
+  set shared_port [ clone ]
   tolserver::start -port $options(server,port) \
+    -shport $shared_port \
     -logfile [file join $tolcomm_dir "tolserver.log"] \
     -compile $options(compile)
@@ -463,4 +471,19 @@
 }   
 
+proc ::tolsh::find_shared_port { port } {
+  puts "::tolsh::find_shared_port $port"
+  set i 0
+  while { $i < 100 } {
+    if { [ catch { socket localhost $port } fd ] } {
+      puts "find_shared_port OK = $port"
+      return $port
+    }
+    incr port
+    incr i
+  }
+  puts "find_shared_port FAIL!"
+  return 0
+}
+
 proc ::tolsh::clone {} {
   variable options
@@ -471,21 +494,28 @@
     lappend tolsh $::argv0
   }
-  eval exec $tolsh -shared &
+  set shared_port [ find_shared_port 20458 ]
+  if { !$shared_port } {
+    puts "::tolsh::clone: could not find a valid port for shared data"
+    exit
+  }
+  eval exec $tolsh -shared $shared_port &
   after 5000            ;# wait for data sharing are created by 'shared'
   puts "exec slave"
-  eval exec $tolsh -slave $options(server,port) &
-}
-
-proc ::tolsh::run_as_shared {} {
+  eval exec $tolsh -slave $options(server,port) -shport $shared_port &
+  return $shared_port
+}
+
+proc ::tolsh::run_as_shared { tolcomm_dir } {
   package require tolshared
-
-  tolshared::start
+  variable options
+
+  tolshared::start $options(shared,port) $tolcomm_dir
 }   
 
-proc ::tolsh::run_as_slave {tolcomm_dir} {
+proc ::tolsh::run_as_slave { tolcomm_dir } {
   variable options
   package require tolslave
 
-  tolslave::start -port $options(server,port) \
+  tolslave::start -port $options(server,port) -shport $options(shared,port) \
     -logfile [file join $tolcomm_dir "tolslave.log"]
   vwait __forever__
