 
 (defun emdocs-connect-client (buffer ip)
   "docstring"
-  (add-to-list emdocs-client-list
-               (make-network-process
-                :name (concat "*emdocs-client:"
-                              (buffer-name buffer) ":" ip "*")
-                ;; TODO: remove the log buffer
-                :buffer (concat "*emdocs-client-log:" (buffer-name buffer) "*")
-                :family 'ipv4
-                :host ip
-                :service +emdocs-http-port+
-                :sentinel (lambda (sock msg)
-                            (emdocs-client-sentinel buffer sock msg))
-                :filter (lambda (sock msg)
-                          (emdocs-client-filter buffer sock msg))
-                :server nil
-                :noquery t)))
+  (make-network-process
+   :name (concat "*emdocs-client:"
+                 (buffer-name buffer) ":" ip "*")
+   ;; TODO: remove the log buffer
+   :buffer (concat "*emdocs-client-log:" (buffer-name buffer) "*")
+   :family 'ipv4
+   :host ip
+   :service +emdocs-http-port+
+   :sentinel (lambda (sock msg)
+               (emdocs-client-sentinel buffer sock msg))
+   :filter (lambda (sock msg)
+             (emdocs-client-filter buffer sock msg))
+   :server nil
+   :noquery t))
 
 (defun emdocs-attach-sockets-to-buffer (buffer ip)
   "docstring"
   (let ((my-ip (emdocs-get-internal-ip-address)))
     (if my-ip
         (progn
-          (emdocs-setup-server buffer my-ip)
-          (unless (string-equal ip "localhost")
-            (make-local-variable emdocs-client-list)
+          (make-local-variable 'emdocs-server)
+          (setq emdocs-server nil)
+          (setq emdocs-server           ; the setq is required, not sure why
+                (condition-case emdocs-server
+                    (emdocs-setup-server buffer my-ip)
+                  (file-error 'no-conn)))
+          (if (eq emdocs-server 'no-conn)
+              (progn
+                (message "Server could not connect: exiting.")
+                (setq emdocs-mode nil)
+                (emdocs-disconnect))
+            (make-local-variable 'emdocs-client-list)
             (setq emdocs-client-list nil)
-            (emdocs-connect-client buffer ip)))
-      (error "Not connected to internet: exiting."))))
+            (unless (or (string-equal ip "localhost")
+                        (string-equal ip ""))
+              (setq emdocs-client-list
+                    (condition-case emdocs-client-list
+                        (cons (emdocs-connect-client buffer ip) nil)
+                      (file-error 'no-conn))))
+            (when (eq emdocs-client-list 'no-conn)
+              (message "Client could not connect: exiting.")
+              (setq emdocs-mode nil)
+              (emdocs-disconnect))))
+      (message "Not connected to internet: exiting.")
+      (setq emdocs-mode nil)
+      (emdocs-disconnect))))
 
 (defun emdocs-connect ()
   "docstring"


(defun emdocs-disconnect ()
  "docstring"
  (when emdocs-server
      (delete-process emdocs-server)))

;;; define mode



(defun emdocs-setup-server (buffer my-ip)
  "docstring"
  (make-network-process
   :name (concat "*emdocs-server:" (buffer-name buffer) "*")
   ;; TODO: remove the log buffer
   :buffer (concat "*emdocs-server-log:" (buffer-name buffer) "*")
   :family 'ipv4
   :host my-ip
   :service +emdocs-http-port+
   :sentinel (lambda (sock msg)
               (emdocs-server-sentinel buffer sock msg))
   :filter (lambda (sock msg)
             (emdocs-server-filter buffer sock msg))
   :server t
   :noquery t))

(defun emdocs-client-sentinel (buffer sock msg)
  "docstring")
