+ /* connect to the simulator */
+ if (SOCKET_ERROR == connect(sock, (struct sockaddr *)&sin, sizeof(sin)))
+ {
+ /* if failed then wait 1 second & try again
+ do this for 10 secs only */
+ if (retry < 10)
+ {
+ if ( !retry )
+ simPid = execSimulator(args, nargs);
+ retry ++;
+ Sleep(1000);
+ goto try_connect;
+ }
+ perror("connect failed :");
+ exit(1);
+ }
+
+ fh = _open_osfhandle((intptr_t)sock, _O_TEXT);
+ if (-1 == fh)
+ {
+ perror("cannot _open_osfhandle");
+ exit(1);
+ }
+
+ /* go the socket now turn it into a file handle */
+ if (!(simin = fdopen(fh, "r")))
+ {
+ perror("cannot open socket for read");
+ exit(1);
+ }
+
+ fh = _open_osfhandle((intptr_t)sock, _O_TEXT);
+ if (-1 == fh)
+ {
+ perror("cannot _open_osfhandle");
+ exit(1);
+ }
+
+ if (!(simout = fdopen(fh, "w")))
+ {
+ perror("cannot open socket for write");
+ exit(1);
+ }
+ /* now that we have opened, wait for the prompt */
+ waitForSim(200, NULL);
+ simactive = 1;
+}
+#else
+static int execSimulator(char **args, int nargs)
+{
+ if ((simPid = fork()))
+ {
+ Dprintf(D_simi, ("simi: simulator pid %d\n",(int) simPid));
+ }
+ else
+ {
+ /* we are in the child process : start the simulator */
+ signal(SIGINT , SIG_IGN );
+ signal(SIGABRT, SIG_IGN );
+ signal(SIGHUP , SIG_IGN );
+ signal(SIGCHLD, SIG_IGN );
+
+ if (execvp(args[0],args) < 0)
+ {
+ perror("cannot exec simulator");
+ exit(1);
+ }
+ }
+ return simPid;
+}
+
+void openSimulator (char **args, int nargs)
+{
+ struct sockaddr_in sin;
+ int retry = 0;
+ int i;
+ Dprintf(D_simi, ("simi: openSimulator\n"));
+#ifdef SDCDB_DEBUG
+ if (D_simi & sdcdbDebug)
+ {
+ printf("simi: openSimulator: ");
+ for (i=0; i < nargs; i++ )
+ {
+ printf("arg%d: %s ",i,args[i]);
+ }
+ printf("\n");
+ }
+#endif
+ invalidateCache(XMEM_CACHE);
+ invalidateCache(IMEM_CACHE);
+ invalidateCache(SREG_CACHE);
+
+ if ((sock = socket(AF_INET,SOCK_STREAM,0)) < 0)
+ {
+ perror("cannot create socket");
+ exit(1);
+ }
+