         mdb node1 remote>>
         [ [ check-node ] keep ]
         [ drop f ] if*  :> node2
-
         node1 [ acc push ] when*
         node2 [ acc push ] when*
         mdb acc nodelist>table >>nodes drop 
-    ] with-destructors ; 
+    ] with-destructors ;
+
+ERROR: mongod-connection-error address message ;
               
 : mdb-open ( mdb -- mdb-connection )
-    clone [ <mdb-connection> ] keep
-    master-node open-connection
-    [ authenticate-connection ] keep ; 
+    clone [ verify-nodes ] [ <mdb-connection> ] [ ] tri
+    master-node [
+        open-connection [ authenticate-connection ] keep
+    ] [ drop nip address>> "Could not open connection to mongod" mongod-connection-error ] recover ;
 
 : mdb-close ( mdb-connection -- )
-     [ dispose f ] change-handle drop ;
+     [ [ dispose ] when* f ] change-handle drop ;
 
 M: mdb-connection dispose
      mdb-close ;

USING: accessors arrays assocs byte-vectors checksums
checksums.md5 constructors destructors fry hashtables
io.encodings.binary io.encodings.string io.encodings.utf8
io.sockets io.streams.duplex kernel locals math math.parser
mongodb.cmd mongodb.msg namespaces sequences
splitting ;
IN: mongodb.connection

: md5-checksum ( string -- digest )

   ] with-connection ; inline

: open-connection ( mdb-connection node -- mdb-connection )
    [ >>node ] [ address>> ] bi
    [ >>remote ] keep binary <client>
    [ >>handle ] dip >>local 4096 <byte-vector> >>buffer ;

: get-ismaster ( -- result )
    "admin.$cmd" H{ { "ismaster" 1 } } send-query-1result ; 
