# Start netcat listening on an ephemeral port (0 means kernel picks port)
     #    It will wait for a connection, then write that data to output_file
-
-    nc -l 0 > output_file &
+    # -d is needed or else it will give up too early.
+    nc -d -l 0 > output_file &
 
     # pid of the nc running in the background
     NCPID=$!