+/* If true, then GDB has requested noack mode. */
+int noack_mode = 0;
+/* If true, then we tell GDB to use noack mode by default. */
+int transport_is_reliable = 0;
+
+#ifdef USE_WIN32API
+# define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
+# define write(fd, buf, len) send (fd, (char *) buf, len, 0)
+#endif
+
+int
+gdb_connected (void)
+{
+ return remote_desc != INVALID_DESCRIPTOR;
+}
+
+static void
+enable_async_notification (int fd)
+{
+#if defined(F_SETFL) && defined (FASYNC)
+ int save_fcntl_flags;
+
+ save_fcntl_flags = fcntl (fd, F_GETFL, 0);
+ fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC);
+#if defined (F_SETOWN)
+ fcntl (fd, F_SETOWN, getpid ());
+#endif
+#endif
+}
+
+static int
+handle_accept_event (int err, gdb_client_data client_data)
+{
+ struct sockaddr_in sockaddr;
+ socklen_t tmp;
+
+ if (debug_threads)
+ fprintf (stderr, "handling possible accept event\n");
+
+ tmp = sizeof (sockaddr);
+ remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &tmp);
+ if (remote_desc == -1)
+ perror_with_name ("Accept failed");
+
+ /* Enable TCP keep alive process. */
+ tmp = 1;
+ setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
+ (char *) &tmp, sizeof (tmp));
+
+ /* Tell TCP not to delay small packets. This greatly speeds up
+ interactive response. */
+ tmp = 1;
+ setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
+ (char *) &tmp, sizeof (tmp));
+
+#ifndef USE_WIN32API
+ close (listen_desc); /* No longer need this */
+
+ signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
+ exits when the remote side dies. */
+#else
+ closesocket (listen_desc); /* No longer need this */
+#endif
+
+ delete_file_handler (listen_desc);
+
+ /* Convert IP address to string. */
+ fprintf (stderr, "Remote debugging from host %s\n",
+ inet_ntoa (sockaddr.sin_addr));
+
+ enable_async_notification (remote_desc);
+
+ /* Register the event loop handler. */
+ add_file_handler (remote_desc, handle_serial_event, NULL);
+
+ /* We have a new GDB connection now. If we were disconnected
+ tracing, there's a window where the target could report a stop
+ event to the event loop, and since we have a connection now, we'd
+ try to send vStopped notifications to GDB. But, don't do that
+ until GDB as selected all-stop/non-stop, and has queried the
+ threads' status ('?'). */
+ target_async (0);
+
+ return 0;
+}
+