+#ifndef NO_SOCKETS_IN_FILE_SYSTEM
+ /* Implicit local socket. */
+ s = set_local_socket ();
+ if (s != INVALID_SOCKET)
+ return s;
+#endif
+
+ /* Implicit server file. */
+ server_file = "server";
+ s = set_tcp_socket ();
+ if ((s != INVALID_SOCKET) || alternate_editor)
+ return s;
+
+ /* No implicit or explicit socket, and no alternate editor. */
+ message (TRUE, "%s: No socket or alternate editor. Please use:\n\n"
+#ifndef NO_SOCKETS_IN_FILE_SYSTEM
+"\t--socket-name\n"
+#endif
+"\t--server-file (or environment variable EMACS_SERVER_FILE)\n\
+\t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n",
+ progname);
+ exit (EXIT_FAILURE);
+}
+
+#ifdef WINDOWSNT
+FARPROC set_fg; /* Pointer to AllowSetForegroundWindow. */
+FARPROC get_wc; /* Pointer to RealGetWindowClassA. */
+
+BOOL CALLBACK
+w32_find_emacs_process (hWnd, lParam)
+ HWND hWnd;
+ LPARAM lParam;
+{
+ DWORD pid;
+ char class[6];
+
+ /* Reject any window not of class "Emacs". */
+ if (! get_wc (hWnd, class, sizeof (class))
+ || strcmp (class, "Emacs"))
+ return TRUE;
+
+ /* We only need the process id, not the thread id. */
+ (void) GetWindowThreadProcessId (hWnd, &pid);
+
+ /* Not the one we're looking for. */
+ if (pid != (DWORD) emacs_pid) return TRUE;
+
+ /* OK, let's raise it. */
+ set_fg (emacs_pid);
+
+ /* Stop enumeration. */
+ return FALSE;
+}
+
+/*
+ * Search for a window of class "Emacs" and owned by a process with
+ * process id = emacs_pid. If found, allow it to grab the focus.
+ */
+void
+w32_give_focus ()
+{
+ HMODULE hUser32;
+
+ /* It shouldn't happen when dealing with TCP sockets. */
+ if (!emacs_pid) return;
+
+ if (!(hUser32 = LoadLibrary ("user32.dll"))) return;
+
+ /* Modern Windows restrict which processes can set the foreground window.
+ emacsclient can allow Emacs to grab the focus by calling the function
+ AllowSetForegroundWindow. Unfortunately, older Windows (W95, W98 and
+ NT) lack this function, so we have to check its availability. */
+ if ((set_fg = GetProcAddress (hUser32, "AllowSetForegroundWindow"))
+ && (get_wc = GetProcAddress (hUser32, "RealGetWindowClassA")))
+ EnumWindows (w32_find_emacs_process, (LPARAM) 0);
+
+ FreeLibrary (hUser32);
+}
+#endif
+
+int
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ HSOCKET s;
+ int i, rl, needlf = 0;
+ char *cwd;
+ char string[BUFSIZ+1];
+
+ progname = argv[0];
+
+ /* Process options. */
+ decode_options (argc, argv);
+
+ if ((argc - optind < 1) && !eval)
+ {
+ message (TRUE, "%s: file name or argument required\nTry `%s --help' for more information\n",
+ progname, progname);
+ exit (EXIT_FAILURE);
+ }
+
+ if ((s = set_socket ()) == INVALID_SOCKET)
+ fail (argc, argv);
+