Further server optimizations:
- merged request and reply structures
- build requests directly into the buffer to avoid a copy

diff --git a/include/server.h b/include/server.h
index b2f5426..f7084768 100644
--- a/include/server.h
+++ b/include/server.h
@@ -10,144 +10,122 @@
 #include <stdlib.h>
 #include <time.h>
 
-/* message header as sent on the wire */
-struct header
-{
-    unsigned int  len;     /* total msg length (including this header) */
-    unsigned int  type;    /* msg type */
-};
-
-/* max msg length (including the header) */
-#define MAX_MSG_LENGTH  16384
-
-/* data structure used to pass an fd with sendmsg/recvmsg */
-struct cmsg_fd
-{
-    int len;   /* sizeof structure */
-    int level; /* SOL_SOCKET */
-    int type;  /* SCM_RIGHTS */
-    int fd;    /* fd to pass */
-};
-
 /* Request structures */
 
-/* following are the definitions of all the client<->server  */
-/* communication format; requests are from client to server, */
-/* replies are from server to client. All requests must have */
-/* a corresponding structure; the replies can be empty in    */
-/* which case it isn't necessary to define a structure.      */
+/* Following are the definitions of all the client<->server   */
+/* communication format; if you make any change in this file, */
+/* you must run tools/make_requests again. */
+
+
+/* These empty macros are used by tools/make_requests */
+/* to generate the request/reply tracing functions */
+#define IN  /*nothing*/
+#define OUT /*nothing*/
 
 
 /* Create a new process from the context of the parent */
 struct new_process_request
 {
-    int          inherit;      /* inherit flag */
-    int          inherit_all;  /* inherit all handles from parent */
-    int          create_flags; /* creation flags */
-    int          start_flags;  /* flags from startup info */
-    int          hstdin;       /* handle for stdin */
-    int          hstdout;      /* handle for stdout */
-    int          hstderr;      /* handle for stderr */
-    int          cmd_show;     /* main window show mode */
-    void*        env_ptr;      /* pointer to environment (FIXME: hack) */
-    char         cmd_line[0];  /* command line */
-};
-struct new_process_reply
-{
-    void*        pid;          /* process id */
-    int          handle;       /* process handle (in the current process) */
+    IN  int          inherit;      /* inherit flag */
+    IN  int          inherit_all;  /* inherit all handles from parent */
+    IN  int          create_flags; /* creation flags */
+    IN  int          start_flags;  /* flags from startup info */
+    IN  int          hstdin;       /* handle for stdin */
+    IN  int          hstdout;      /* handle for stdout */
+    IN  int          hstderr;      /* handle for stderr */
+    IN  int          cmd_show;     /* main window show mode */
+    IN  void*        env_ptr;      /* pointer to environment (FIXME: hack) */
+    OUT void*        pid;          /* process id */
+    OUT int          handle;       /* process handle (in the current process) */
+    IN  char         cmdline[1];   /* command line */
 };
 
 
 /* Create a new thread from the context of the parent */
 struct new_thread_request
 {
-    void*        pid;          /* process id for the new thread */
-    int          suspend;      /* new thread should be suspended on creation */ 
-    int          inherit;      /* inherit flag */
-};
-struct new_thread_reply
-{
-    void*        tid;          /* thread id */
-    int          handle;       /* thread handle (in the current process) */
+    IN  void*        pid;          /* process id for the new thread */
+    IN  int          suspend;      /* new thread should be suspended on creation */ 
+    IN  int          inherit;      /* inherit flag */
+    OUT void*        tid;          /* thread id */
+    OUT int          handle;       /* thread handle (in the current process) */
 };
 
 
 /* Set the server debug level */
 struct set_debug_request
 {
-    int          level;        /* New debug level */
+    IN  int          level;        /* New debug level */
 };
 
 
 /* Initialize a process; called from the new process context */
 struct init_process_request
 {
-    int          dummy;
-};
-struct init_process_reply
-{
-    int          start_flags;  /* flags from startup info */
-    int          hstdin;       /* handle for stdin */
-    int          hstdout;      /* handle for stdout */
-    int          hstderr;      /* handle for stderr */
-    int          cmd_show;     /* main window show mode */
-    void*        env_ptr;      /* pointer to environment (FIXME: hack) */
-    char         cmdline[0];   /* command line */
+    OUT int          start_flags;  /* flags from startup info */
+    OUT int          hstdin;       /* handle for stdin */
+    OUT int          hstdout;      /* handle for stdout */
+    OUT int          hstderr;      /* handle for stderr */
+    OUT int          cmd_show;     /* main window show mode */
+    OUT void*        env_ptr;      /* pointer to environment (FIXME: hack) */
+    OUT char         cmdline[1];   /* command line */
 };
 
 
 /* Initialize a thread; called from the child after fork()/clone() */
 struct init_thread_request
 {
-    int          unix_pid;     /* Unix pid of new thread */
-    void*        teb;          /* TEB of new thread (in thread address space) */
+    IN  int          unix_pid;     /* Unix pid of new thread */
+    IN  void*        teb;          /* TEB of new thread (in thread address space) */
+    OUT void*        pid;          /* process id of the new thread's process */
+    OUT void*        tid;          /* thread id of the new thread */
 };
-struct init_thread_reply
+
+
+/* Retrieve the thread buffer file descriptor */
+/* The reply to this request is the first thing a newly */
+/* created thread gets (without having to request it) */
+struct get_thread_buffer_request
 {
-    void*        pid;          /* process id of the new thread's process */
-    void*        tid;          /* thread id of the new thread */
+    IN  int          dummy;
 };
 
 
 /* Terminate a process */
 struct terminate_process_request
 {
-    int          handle;       /* process handle to terminate */
-    int          exit_code;    /* process exit code */
+    IN  int          handle;       /* process handle to terminate */
+    IN  int          exit_code;    /* process exit code */
 };
 
 
 /* Terminate a thread */
 struct terminate_thread_request
 {
-    int          handle;       /* thread handle to terminate */
-    int          exit_code;    /* thread exit code */
+    IN  int          handle;       /* thread handle to terminate */
+    IN  int          exit_code;    /* thread exit code */
 };
 
 
 /* Retrieve information about a process */
 struct get_process_info_request
 {
-    int          handle;       /* process handle */
-};
-struct get_process_info_reply
-{
-    void*        pid;              /* server process id */
-    int          exit_code;        /* process exit code */
-    int          priority;         /* priority class */
-    int          process_affinity; /* process affinity mask */
-    int          system_affinity;  /* system affinity mask */
+    IN  int          handle;       /* process handle */
+    OUT void*        pid;              /* server process id */
+    OUT int          exit_code;        /* process exit code */
+    OUT int          priority;         /* priority class */
+    OUT int          process_affinity; /* process affinity mask */
+    OUT int          system_affinity;  /* system affinity mask */
 };
 
 
 /* Set a process informations */
 struct set_process_info_request
 {
-    int          handle;       /* process handle */
-    int          mask;         /* setting mask (see below) */
-    int          priority;     /* priority class */
-    int          affinity;     /* affinity mask */
+    IN  int          handle;       /* process handle */
+    IN  int          mask;         /* setting mask (see below) */
+    IN  int          priority;     /* priority class */
+    IN  int          affinity;     /* affinity mask */
 };
 #define SET_PROCESS_INFO_PRIORITY 0x01
 #define SET_PROCESS_INFO_AFFINITY 0x02
@@ -156,23 +134,20 @@
 /* Retrieve information about a thread */
 struct get_thread_info_request
 {
-    int          handle;       /* thread handle */
-};
-struct get_thread_info_reply
-{
-    void*        tid;          /* server thread id */
-    int          exit_code;    /* thread exit code */
-    int          priority;     /* thread priority level */
+    IN  int          handle;       /* thread handle */
+    OUT void*        tid;          /* server thread id */
+    OUT int          exit_code;    /* thread exit code */
+    OUT int          priority;     /* thread priority level */
 };
 
 
 /* Set a thread informations */
 struct set_thread_info_request
 {
-    int          handle;       /* thread handle */
-    int          mask;         /* setting mask (see below) */
-    int          priority;     /* priority class */
-    int          affinity;     /* affinity mask */
+    IN  int          handle;       /* thread handle */
+    IN  int          mask;         /* setting mask (see below) */
+    IN  int          priority;     /* priority class */
+    IN  int          affinity;     /* affinity mask */
 };
 #define SET_THREAD_INFO_PRIORITY 0x01
 #define SET_THREAD_INFO_AFFINITY 0x02
@@ -181,29 +156,23 @@
 /* Suspend a thread */
 struct suspend_thread_request
 {
-    int          handle;       /* thread handle */
-};
-struct suspend_thread_reply
-{
-    int          count;        /* new suspend count */
+    IN  int          handle;       /* thread handle */
+    OUT int          count;        /* new suspend count */
 };
 
 
 /* Resume a thread */
 struct resume_thread_request
 {
-    int          handle;       /* thread handle */
-};
-struct resume_thread_reply
-{
-    int          count;        /* new suspend count */
+    IN  int          handle;       /* thread handle */
+    OUT int          count;        /* new suspend count */
 };
 
 
 /* Debugger support: freeze / unfreeze */
 struct debugger_request
 {
-    int          op;           /* operation type */
+    IN  int          op;           /* operation type */
 };
 
 enum debugger_op { DEBUGGER_FREEZE_ALL, DEBUGGER_UNFREEZE_ALL };
@@ -212,83 +181,78 @@
 /* Queue an APC for a thread */
 struct queue_apc_request
 {
-    int          handle;       /* thread handle */
-    void*        func;         /* function to call */
-    void*        param;        /* param for function to call */
+    IN  int          handle;       /* thread handle */
+    IN  void*        func;         /* function to call */
+    IN  void*        param;        /* param for function to call */
+};
+
+
+/* Get list of APC to call */
+struct get_apcs_request
+{
+    OUT int          count;        /* number of apcs */
+    OUT void*        apcs[1];      /* async procedures to call */
 };
 
 
 /* Close a handle for the current process */
 struct close_handle_request
 {
-    int          handle;       /* handle to close */
+    IN  int          handle;       /* handle to close */
 };
 
 
 /* Get information about a handle */
 struct get_handle_info_request
 {
-    int          handle;       /* handle we are interested in */
-};
-struct get_handle_info_reply
-{
-    int          flags;        /* handle flags */
+    IN  int          handle;       /* handle we are interested in */
+    OUT int          flags;        /* handle flags */
 };
 
 
 /* Set a handle information */
 struct set_handle_info_request
 {
-    int          handle;       /* handle we are interested in */
-    int          flags;        /* new handle flags */
-    int          mask;         /* mask for flags to set */
+    IN  int          handle;       /* handle we are interested in */
+    IN  int          flags;        /* new handle flags */
+    IN  int          mask;         /* mask for flags to set */
 };
 
 
 /* Duplicate a handle */
 struct dup_handle_request
 {
-    int          src_process;  /* src process handle */
-    int          src_handle;   /* src handle to duplicate */
-    int          dst_process;  /* dst process handle */
-    unsigned int access;       /* wanted access rights */
-    int          inherit;      /* inherit flag */
-    int          options;      /* duplicate options (see below) */
+    IN  int          src_process;  /* src process handle */
+    IN  int          src_handle;   /* src handle to duplicate */
+    IN  int          dst_process;  /* dst process handle */
+    IN  unsigned int access;       /* wanted access rights */
+    IN  int          inherit;      /* inherit flag */
+    IN  int          options;      /* duplicate options (see below) */
+    OUT int          handle;       /* duplicated handle in dst process */
 };
 #define DUP_HANDLE_CLOSE_SOURCE  DUPLICATE_CLOSE_SOURCE
 #define DUP_HANDLE_SAME_ACCESS   DUPLICATE_SAME_ACCESS
 #define DUP_HANDLE_MAKE_GLOBAL   0x80000000  /* Not a Windows flag */
-struct dup_handle_reply
-{
-    int          handle;       /* duplicated handle in dst process */
-};
 
 
 /* Open a handle to a process */
 struct open_process_request
 {
-    void*        pid;          /* process id to open */
-    unsigned int access;       /* wanted access rights */
-    int          inherit;      /* inherit flag */
-};
-struct open_process_reply
-{
-    int          handle;       /* handle to the process */
+    IN  void*        pid;          /* process id to open */
+    IN  unsigned int access;       /* wanted access rights */
+    IN  int          inherit;      /* inherit flag */
+    OUT int          handle;       /* handle to the process */
 };
 
 
 /* Wait for handles */
 struct select_request
 {
-    int          count;        /* handles count */
-    int          flags;        /* wait flags (see below) */
-    int          timeout;      /* timeout in ms */
-    int          handles[0];   /* handles to select on */
-};
-struct select_reply
-{
-    int          signaled;     /* signaled handle */
-    void*        apcs[0];      /* async procedures to call */
+    IN  int          count;        /* handles count */
+    IN  int          flags;        /* wait flags (see below) */
+    IN  int          timeout;      /* timeout in ms */
+    OUT int          signaled;     /* signaled handle */
+    IN  int          handles[1];   /* handles to select on */
 };
 #define SELECT_ALL       1
 #define SELECT_ALERTABLE 2
@@ -298,21 +262,18 @@
 /* Create an event */
 struct create_event_request
 {
-    int          manual_reset;  /* manual reset event */
-    int          initial_state; /* initial state of the event */
-    int          inherit;       /* inherit flag */
-    char         name[0];       /* event name */
-};
-struct create_event_reply
-{
-    int          handle;        /* handle to the event */
+    IN  int          manual_reset;  /* manual reset event */
+    IN  int          initial_state; /* initial state of the event */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle;        /* handle to the event */
+    IN  char         name[1];       /* event name */
 };
 
 /* Event operation */
 struct event_op_request
 {
-    int           handle;       /* handle to event */
-    int           op;           /* event operation (see below) */
+    IN  int           handle;       /* handle to event */
+    IN  int           op;           /* event operation (see below) */
 };
 enum event_op { PULSE_EVENT, SET_EVENT, RESET_EVENT };
 
@@ -320,278 +281,248 @@
 /* Open an event */
 struct open_event_request
 {
-    unsigned int access;        /* wanted access rights */
-    int          inherit;       /* inherit flag */
-    char         name[0];       /* object name */
-};
-struct open_event_reply
-{
-    int          handle;        /* handle to the event */
+    IN  unsigned int access;        /* wanted access rights */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle;        /* handle to the event */
+    IN  char         name[1];       /* object name */
 };
 
 
 /* Create a mutex */
 struct create_mutex_request
 {
-    int          owned;         /* initially owned? */
-    int          inherit;       /* inherit flag */
-    char         name[0];       /* mutex name */
-};
-struct create_mutex_reply
-{
-    int          handle;        /* handle to the mutex */
+    IN  int          owned;         /* initially owned? */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle;        /* handle to the mutex */
+    IN  char         name[1];       /* mutex name */
 };
 
 
 /* Release a mutex */
 struct release_mutex_request
 {
-    int          handle;        /* handle to the mutex */
+    IN  int          handle;        /* handle to the mutex */
 };
 
 
 /* Open a mutex */
 struct open_mutex_request
 {
-    unsigned int access;        /* wanted access rights */
-    int          inherit;       /* inherit flag */
-    char         name[0];       /* object name */
-};
-struct open_mutex_reply
-{
-    int          handle;        /* handle to the mutex */
+    IN  unsigned int access;        /* wanted access rights */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle;        /* handle to the mutex */
+    IN  char         name[1];       /* object name */
 };
 
 
 /* Create a semaphore */
 struct create_semaphore_request
 {
-    unsigned int initial;       /* initial count */
-    unsigned int max;           /* maximum count */
-    int          inherit;       /* inherit flag */
-    char         name[0];       /* semaphore name */
-};
-struct create_semaphore_reply
-{
-    int          handle;        /* handle to the semaphore */
+    IN  unsigned int initial;       /* initial count */
+    IN  unsigned int max;           /* maximum count */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle;        /* handle to the semaphore */
+    IN  char         name[1];       /* semaphore name */
 };
 
 
 /* Release a semaphore */
 struct release_semaphore_request
 {
-    int          handle;        /* handle to the semaphore */
-    unsigned int count;         /* count to add to semaphore */
-};
-struct release_semaphore_reply
-{
-    unsigned int prev_count;    /* previous semaphore count */
+    IN  int          handle;        /* handle to the semaphore */
+    IN  unsigned int count;         /* count to add to semaphore */
+    OUT unsigned int prev_count;    /* previous semaphore count */
 };
 
 
 /* Open a semaphore */
 struct open_semaphore_request
 {
-    unsigned int access;        /* wanted access rights */
-    int          inherit;       /* inherit flag */
-    char         name[0];       /* object name */
-};
-struct open_semaphore_reply
-{
-    int          handle;        /* handle to the semaphore */
+    IN  unsigned int access;        /* wanted access rights */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle;        /* handle to the semaphore */
+    IN  char         name[1];       /* object name */
 };
 
 
 /* Create a file */
 struct create_file_request
 {
-    unsigned int access;        /* wanted access rights */
-    int          inherit;       /* inherit flag */
-    unsigned int sharing;       /* sharing flags */
-    int          create;        /* file create action */
-    unsigned int attrs;         /* file attributes for creation */
-    char         name[0];       /* file name */
+    IN  unsigned int access;        /* wanted access rights */
+    IN  int          inherit;       /* inherit flag */
+    IN  unsigned int sharing;       /* sharing flags */
+    IN  int          create;        /* file create action */
+    IN  unsigned int attrs;         /* file attributes for creation */
+    OUT int          handle;        /* handle to the file */
+    IN  char         name[1];       /* file name */
 };
-struct create_file_reply
+
+
+/* Allocate a file handle for a Unix fd */
+struct alloc_file_handle_request
 {
-    int          handle;        /* handle to the file */
+    IN  unsigned int access;        /* wanted access rights */
+    OUT int          handle;        /* handle to the file */
 };
 
 
 /* Get a Unix fd to read from a file */
 struct get_read_fd_request
 {
-    int          handle;        /* handle to the file */
+    IN  int          handle;        /* handle to the file */
 };
 
 
 /* Get a Unix fd to write to a file */
 struct get_write_fd_request
 {
-    int          handle;        /* handle to the file */
+    IN  int          handle;        /* handle to the file */
 };
 
 
 /* Set a file current position */
 struct set_file_pointer_request
 {
-    int          handle;        /* handle to the file */
-    int          low;           /* position low word */
-    int          high;          /* position high word */
-    int          whence;        /* whence to seek */
-};
-struct set_file_pointer_reply
-{
-    int          low;           /* new position low word */
-    int          high;          /* new position high word */
+    IN  int          handle;        /* handle to the file */
+    IN  int          low;           /* position low word */
+    IN  int          high;          /* position high word */
+    IN  int          whence;        /* whence to seek */
+    OUT int          new_low;       /* new position low word */
+    OUT int          new_high;      /* new position high word */
 };
 
 
 /* Truncate (or extend) a file */
 struct truncate_file_request
 {
-    int          handle;        /* handle to the file */
+    IN  int          handle;        /* handle to the file */
 };
 
 
 /* Set a file access and modification times */
 struct set_file_time_request
 {
-    int          handle;        /* handle to the file */
-    time_t       access_time;   /* last access time */
-    time_t       write_time;    /* last write time */
+    IN  int          handle;        /* handle to the file */
+    IN  time_t       access_time;   /* last access time */
+    IN  time_t       write_time;    /* last write time */
 };
 
 
 /* Flush a file buffers */
 struct flush_file_request
 {
-    int          handle;        /* handle to the file */
+    IN  int          handle;        /* handle to the file */
 };
 
 
 /* Get information about a file */
 struct get_file_info_request
 {
-    int          handle;        /* handle to the file */
-};
-struct get_file_info_reply
-{
-    int          type;          /* file type */
-    int          attr;          /* file attributes */
-    time_t       access_time;   /* last access time */
-    time_t       write_time;    /* last write time */
-    int          size_high;     /* file size */
-    int          size_low;      /* file size */
-    int          links;         /* number of links */
-    int          index_high;    /* unique index */
-    int          index_low;     /* unique index */
-    unsigned int serial;        /* volume serial number */
+    IN  int          handle;        /* handle to the file */
+    OUT int          type;          /* file type */
+    OUT int          attr;          /* file attributes */
+    OUT time_t       access_time;   /* last access time */
+    OUT time_t       write_time;    /* last write time */
+    OUT int          size_high;     /* file size */
+    OUT int          size_low;      /* file size */
+    OUT int          links;         /* number of links */
+    OUT int          index_high;    /* unique index */
+    OUT int          index_low;     /* unique index */
+    OUT unsigned int serial;        /* volume serial number */
 };
 
 
 /* Lock a region of a file */
 struct lock_file_request
 {
-    int          handle;        /* handle to the file */
-    unsigned int offset_low;    /* offset of start of lock */
-    unsigned int offset_high;   /* offset of start of lock */
-    unsigned int count_low;     /* count of bytes to lock */
-    unsigned int count_high;    /* count of bytes to lock */
+    IN  int          handle;        /* handle to the file */
+    IN  unsigned int offset_low;    /* offset of start of lock */
+    IN  unsigned int offset_high;   /* offset of start of lock */
+    IN  unsigned int count_low;     /* count of bytes to lock */
+    IN  unsigned int count_high;    /* count of bytes to lock */
 };
 
 
 /* Unlock a region of a file */
 struct unlock_file_request
 {
-    int          handle;        /* handle to the file */
-    unsigned int offset_low;    /* offset of start of unlock */
-    unsigned int offset_high;   /* offset of start of unlock */
-    unsigned int count_low;     /* count of bytes to unlock */
-    unsigned int count_high;    /* count of bytes to unlock */
+    IN  int          handle;        /* handle to the file */
+    IN  unsigned int offset_low;    /* offset of start of unlock */
+    IN  unsigned int offset_high;   /* offset of start of unlock */
+    IN  unsigned int count_low;     /* count of bytes to unlock */
+    IN  unsigned int count_high;    /* count of bytes to unlock */
 };
 
 
 /* Create an anonymous pipe */
 struct create_pipe_request
 {
-    int          inherit;       /* inherit flag */
-};
-struct create_pipe_reply
-{
-    int          handle_read;   /* handle to the read-side of the pipe */
-    int          handle_write;  /* handle to the write-side of the pipe */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle_read;   /* handle to the read-side of the pipe */
+    OUT int          handle_write;  /* handle to the write-side of the pipe */
 };
 
 
 /* Allocate a console for the current process */
 struct alloc_console_request
 {
-    unsigned int access;        /* wanted access rights */
-    int          inherit;       /* inherit flag */
-};
-struct alloc_console_reply
-{
-    int          handle_in;     /* handle to console input */
-    int          handle_out;    /* handle to console output */
+    IN  unsigned int access;        /* wanted access rights */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle_in;     /* handle to console input */
+    OUT int          handle_out;    /* handle to console output */
 };
 
 
 /* Free the console of the current process */
 struct free_console_request
 {
-    int dummy;
+    IN  int dummy;
 };
 
 
 /* Open a handle to the process console */
 struct open_console_request
 {
-    int          output;        /* input or output? */
-    unsigned int access;        /* wanted access rights */
-    int          inherit;       /* inherit flag */
-};
-struct open_console_reply
-{
-    int          handle;        /* handle to the console */
+    IN  int          output;        /* input or output? */
+    IN  unsigned int access;        /* wanted access rights */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle;        /* handle to the console */
 };
 
 
 /* Set a console file descriptor */
 struct set_console_fd_request
 {
-    int          handle;        /* handle to the console */
-    int          pid;           /* pid of xterm (hack) */
+    IN  int          handle;        /* handle to the console */
+    IN  int          file_handle;   /* handle of file to use as file descriptor */
+    IN  int          pid;           /* pid of xterm (hack) */
 };
 
 
 /* Get a console mode (input or output) */
 struct get_console_mode_request
 {
-    int          handle;        /* handle to the console */
-};
-struct get_console_mode_reply
-{
-    int          mode;          /* console mode */
+    IN  int          handle;        /* handle to the console */
+    OUT int          mode;          /* console mode */
 };
 
 
 /* Set a console mode (input or output) */
 struct set_console_mode_request
 {
-    int          handle;        /* handle to the console */
-    int          mode;          /* console mode */
+    IN  int          handle;        /* handle to the console */
+    IN  int          mode;          /* console mode */
 };
 
 
 /* Set info about a console (output only) */
 struct set_console_info_request
 {
-    int          handle;        /* handle to the console */
-    int          mask;          /* setting mask (see below) */
-    int          cursor_size;   /* size of cursor (percentage filled) */
-    int          cursor_visible;/* cursor visibility flag */
-    char         title[0];      /* console title */
+    IN  int          handle;        /* handle to the console */
+    IN  int          mask;          /* setting mask (see below) */
+    IN  int          cursor_size;   /* size of cursor (percentage filled) */
+    IN  int          cursor_visible;/* cursor visibility flag */
+    IN  char         title[1];      /* console title */
 };
 #define SET_CONSOLE_INFO_CURSOR 0x01
 #define SET_CONSOLE_INFO_TITLE  0x02
@@ -599,68 +530,53 @@
 /* Get info about a console (output only) */
 struct get_console_info_request
 {
-    int          handle;        /* handle to the console */
-};
-struct get_console_info_reply
-{
-    int          cursor_size;   /* size of cursor (percentage filled) */
-    int          cursor_visible;/* cursor visibility flag */
-    int          pid;           /* pid of xterm (hack) */
-    char         title[0];      /* console title */
+    IN  int          handle;        /* handle to the console */
+    OUT int          cursor_size;   /* size of cursor (percentage filled) */
+    OUT int          cursor_visible;/* cursor visibility flag */
+    OUT int          pid;           /* pid of xterm (hack) */
+    OUT char         title[1];      /* console title */
 };
 
 
 /* Add input records to a console input queue */
 struct write_console_input_request
 {
-    int          handle;        /* handle to the console input */
-    int          count;         /* number of input records */
-/*  INPUT_RECORD records[0]; */ /* input records */
-};
-struct write_console_input_reply
-{
-    int          written;       /* number of records written */
+    IN  int          handle;        /* handle to the console input */
+    IN  int          count;         /* number of input records */
+    OUT int          written;       /* number of records written */
+ /* INPUT_RECORD records[0]; */     /* input records */
 };
 
 /* Fetch input records from a console input queue */
 struct read_console_input_request
 {
-    int          handle;        /* handle to the console input */
-    int          count;         /* max number of records to retrieve */
-    int          flush;         /* flush the retrieved records from the queue? */
-};
-struct read_console_input_reply
-{
-    int dummy;
-/*  INPUT_RECORD records[0]; */ /* input records */
+    IN  int          handle;        /* handle to the console input */
+    IN  int          count;         /* max number of records to retrieve */
+    IN  int          flush;         /* flush the retrieved records from the queue? */
+    OUT int          read;          /* number of records read */
+ /* INPUT_RECORD records[0]; */     /* input records */
 };
 
 
 /* Create a change notification */
 struct create_change_notification_request
 {
-    int          subtree;       /* watch all the subtree */
-    int          filter;        /* notification filter */
-};
-struct create_change_notification_reply
-{
-    int          handle;        /* handle to the change notification */
+    IN  int          subtree;       /* watch all the subtree */
+    IN  int          filter;        /* notification filter */
+    OUT int          handle;        /* handle to the change notification */
 };
 
 
 /* Create a file mapping */
 struct create_mapping_request
 {
-    int          size_high;     /* mapping size */
-    int          size_low;      /* mapping size */
-    int          protect;       /* protection flags (see below) */
-    int          inherit;       /* inherit flag */
-    int          handle;        /* file handle */
-    char         name[0];       /* object name */
-};
-struct create_mapping_reply
-{
-    int          handle;        /* handle to the mapping */
+    IN  int          size_high;     /* mapping size */
+    IN  int          size_low;      /* mapping size */
+    IN  int          protect;       /* protection flags (see below) */
+    IN  int          inherit;       /* inherit flag */
+    IN  int          file_handle;   /* file handle */
+    OUT int          handle;        /* handle to the mapping */
+    IN  char         name[1];       /* object name */
 };
 /* protection flags */
 #define VPROT_READ       0x01
@@ -675,91 +591,70 @@
 /* Open a mapping */
 struct open_mapping_request
 {
-    unsigned int access;        /* wanted access rights */
-    int          inherit;       /* inherit flag */
-    char         name[0];       /* object name */
-};
-struct open_mapping_reply
-{
-    int          handle;        /* handle to the mapping */
+    IN  unsigned int access;        /* wanted access rights */
+    IN  int          inherit;       /* inherit flag */
+    OUT int          handle;        /* handle to the mapping */
+    IN  char         name[1];       /* object name */
 };
 
 
 /* Get information about a file mapping */
 struct get_mapping_info_request
 {
-    int          handle;        /* handle to the mapping */
-};
-struct get_mapping_info_reply
-{
-    int          size_high;     /* mapping size */
-    int          size_low;      /* mapping size */
-    int          protect;       /* protection flags */
+    IN  int          handle;        /* handle to the mapping */
+    OUT int          size_high;     /* mapping size */
+    OUT int          size_low;      /* mapping size */
+    OUT int          protect;       /* protection flags */
 };
 
 
 /* Create a device */
 struct create_device_request
 {
-    unsigned int access;        /* wanted access rights */
-    int          inherit;       /* inherit flag */
-    int          id;            /* client private id */
-};
-struct create_device_reply
-{
-    int          handle;        /* handle to the device */
+    IN  unsigned int access;        /* wanted access rights */
+    IN  int          inherit;       /* inherit flag */
+    IN  int          id;            /* client private id */
+    OUT int          handle;        /* handle to the device */
 };
 
 
 /* Create a snapshot */
 struct create_snapshot_request
 {
-    int          inherit;       /* inherit flag */
-    int          flags;         /* snapshot flags (TH32CS_*) */
-};
-struct create_snapshot_reply
-{
-    int          handle;        /* handle to the snapshot */
+    IN  int          inherit;       /* inherit flag */
+    IN  int          flags;         /* snapshot flags (TH32CS_*) */
+    OUT int          handle;        /* handle to the snapshot */
 };
 
 
 /* Get the next process from a snapshot */
 struct next_process_request
 {
-    int          handle;        /* handle to the snapshot */
-    int          reset;         /* reset snapshot position? */
-};
-struct next_process_reply
-{
-    void*        pid;          /* process id */
-    int          threads;      /* number of threads */
-    int          priority;     /* process priority */
+    IN  int          handle;        /* handle to the snapshot */
+    IN  int          reset;         /* reset snapshot position? */
+    OUT void*        pid;          /* process id */
+    OUT int          threads;      /* number of threads */
+    OUT int          priority;     /* process priority */
 };
 
 
 /* Wait for a debug event */
 struct wait_debug_event_request
 {
-    int          timeout;      /* timeout in ms */
-};
-struct wait_debug_event_reply
-{
-    int          code;         /* event code */
-    void*        pid;          /* process id */
-    void*        tid;          /* thread id */
-    /* followed by the event data (see below) */
+    IN  int          timeout;      /* timeout in ms */
+    OUT int          code;         /* event code */
+    OUT void*        pid;          /* process id */
+    OUT void*        tid;          /* thread id */
+/*  OUT union debug_event_data data; */
 };
 
 
 /* Send a debug event */
 struct send_debug_event_request
 {
-    int          code;         /* event code */
-    /* followed by the event data (see below) */
-};
-struct send_debug_event_reply
-{
-    int          status;       /* event continuation status */
+    IN  int          code;         /* event code */
+    OUT int          status;       /* event continuation status */
+/*  IN  union debug_event_data data; */
 };
 
 
@@ -837,16 +732,16 @@
 /* Continue a debug event */
 struct continue_debug_event_request
 {
-    void*        pid;          /* process id to continue */
-    void*        tid;          /* thread id to continue */
-    int          status;       /* continuation status */
+    IN  void*        pid;          /* process id to continue */
+    IN  void*        tid;          /* thread id to continue */
+    IN  int          status;       /* continuation status */
 };
 
 
 /* Start debugging an existing process */
 struct debug_process_request
 {
-    void*        pid;          /* id of the process to debug */
+    IN  void*        pid;          /* id of the process to debug */
 };
 
 
@@ -860,6 +755,7 @@
     REQ_SET_DEBUG,
     REQ_INIT_PROCESS,
     REQ_INIT_THREAD,
+    REQ_GET_THREAD_BUFFER,
     REQ_TERMINATE_PROCESS,
     REQ_TERMINATE_THREAD,
     REQ_GET_PROCESS_INFO,
@@ -870,6 +766,7 @@
     REQ_RESUME_THREAD,
     REQ_DEBUGGER,
     REQ_QUEUE_APC,
+    REQ_GET_APCS,
     REQ_CLOSE_HANDLE,
     REQ_GET_HANDLE_INFO,
     REQ_SET_HANDLE_INFO,
@@ -886,6 +783,7 @@
     REQ_RELEASE_SEMAPHORE,
     REQ_OPEN_SEMAPHORE,
     REQ_CREATE_FILE,
+    REQ_ALLOC_FILE_HANDLE,
     REQ_GET_READ_FD,
     REQ_GET_WRITE_FD,
     REQ_SET_FILE_POINTER,
@@ -930,31 +828,24 @@
 
 #include "thread.h"
 
-/* make space for some data in the server arguments buffer */
-static inline void *server_add_data( int len )
+/* client communication functions */
+
+/* get a pointer to the request buffer */
+static inline void *get_req_buffer(void)
 {
-    void *old = NtCurrentTeb()->buffer_args;
-    NtCurrentTeb()->buffer_args = (char *)old + len;
-    return old;
+    return NtCurrentTeb()->buffer;
 }
 
-/* maximum remaining size in the server arguments buffer */
-static inline int server_remaining(void)
+/* maximum remaining size in the server buffer */
+static inline int server_remaining( const void *ptr )
 {
-    TEB *teb = NtCurrentTeb();
-    return (char *)teb->buffer + teb->buffer_size - (char *)teb->buffer_args;
+    return (char *)NtCurrentTeb()->buffer + NtCurrentTeb()->buffer_size - (char *)ptr;
 }
 
 extern unsigned int server_call( enum request req );
-extern unsigned int server_call_fd( enum request req, int *fd );
+extern unsigned int server_call_fd( enum request req, int fd_out, int *fd_in );
+extern void server_protocol_error( const char *err, ... );
 
-/* client communication functions */
-extern void CLIENT_ProtocolError( const char *err, ... );
-extern void CLIENT_SendRequest( enum request req, int pass_fd,
-                                int n, ... /* arg_1, len_1, etc. */ );
-extern unsigned int CLIENT_WaitReply( int *len, int *passed_fd,
-                                      int n, ... /* arg_1, len_1, etc. */ );
-extern unsigned int CLIENT_WaitSimpleReply( void *reply, int len, int *passed_fd );
 extern int CLIENT_InitServer(void);
 extern int CLIENT_SetDebug( int level );
 extern int CLIENT_DebuggerRequest( int op );