diff --git a/src/mongo.c b/src/mongo.c
index eb4bf88..29ffc2d 100644
--- a/src/mongo.c
+++ b/src/mongo.c
@@ -279,6 +279,37 @@ static mongo_message *mongo_message_create( int len , int id , int responseTo ,
 
     return mm;
 }
+/**
+ * [mongo_try_reconnect ]
+ * @param  conn [Connection]
+ * @return      [MONGO_OK/MONGO_ERROR]
+ */
+static int mongo_try_reconnect(mongo *conn) {
+    int reconnect_tries = 0;
+    int res;
+    if(conn->err == MONGO_IO_ERROR) {
+RECONNECT:
+        if (MONGO_RECONNECT_TRIES_BEFORE_BAILOUT <= reconnect_tries) {
+            return MONGO_ERROR;
+        }
+        printf("reconnecting---");
+        res = mongo_reconnect(conn);
+        if(res == MONGO_OK) {
+            res = mongo_check_connection(conn);
+            if(res == MONGO_OK) {
+                return res;
+            }
+            else {
+                reconnect_tries += 1;
+                //sleep(MONGO_RECONNECT_WAIT);
+                goto RECONNECT;
+            }
+        }
+        return res;
+    }
+    /* This is to preservce the error returned by prev calls*/
+    return MONGO_ERROR;
+}
 
 /* Always calls bson_free(mm) */
 static int mongo_message_send( mongo *conn, mongo_message *mm ) {
@@ -289,16 +320,25 @@ static int mongo_message_send( mongo *conn, mongo_message *mm ) {
     bson_little_endian32( &head.responseTo, &mm->head.responseTo );
     bson_little_endian32( &head.op, &mm->head.op );
 
+RESEND:
     res = mongo_env_write_socket( conn, &head, sizeof( head ) );
     if( res != MONGO_OK ) {
-        bson_free( mm );
-        return res;
+        res = mongo_try_reconnect(conn)
+;        if(res != MONGO_OK){
+            bson_free( mm );
+            return res;
+        }
+        goto RESEND;
     }
 
     res = mongo_env_write_socket( conn, &mm->data, mm->head.len - sizeof( head ) );
     if( res != MONGO_OK ) {
-        bson_free( mm );
-        return res;
+        res = mongo_try_reconnect(conn);
+        if(res != MONGO_OK) {
+            bson_free( mm );
+            return res;
+        }
+        goto RESEND;
     }
 
     bson_free( mm );
@@ -334,6 +374,8 @@ static int mongo_read_response( mongo *conn, mongo_reply **reply ) {
 
     res = mongo_env_read_socket( conn, &out->objs, len-sizeof( head )-sizeof( fields ) );
     if( res != MONGO_OK ) {
+        /* We cannot resurrect the lost data. Only try to reconnect */
+        mongo_try_reconnect(conn);
         bson_free( out );
         return res;
     }
@@ -410,7 +452,6 @@ MONGO_EXPORT void mongo_init( mongo *conn ) {
 
 MONGO_EXPORT int mongo_client( mongo *conn , const char *host, int port ) {
     mongo_init( conn );
-
     conn->primary = bson_malloc( sizeof( mongo_host_port ) );
     strncpy( conn->primary->host, host, strlen( host ) + 1 );
     conn->primary->port = port;
@@ -435,7 +476,6 @@ MONGO_EXPORT int mongo_connect( mongo *conn , const char *host, int port ) {
 
 MONGO_EXPORT void mongo_replica_set_init( mongo *conn, const char *name ) {
     mongo_init( conn );
-
     conn->replica_set = bson_malloc( sizeof( mongo_replica_set ) );
     conn->replica_set->primary_connected = 0;
     conn->replica_set->seeds = NULL;
@@ -676,10 +716,19 @@ MONGO_EXPORT int mongo_replset_connect( mongo *conn ) {
 }
 
 MONGO_EXPORT int mongo_set_op_timeout( mongo *conn, int millis ) {
+    int ret;
+RETRY:
     conn->op_timeout_ms = millis;
-    if( conn->sock && conn->connected )
-        mongo_env_set_socket_op_timeout( conn, millis );
-
+    if( conn->sock && conn->connected ) {
+        ret = mongo_env_set_socket_op_timeout( conn, millis );
+        if(ret != MONGO_OK) {
+            ret = mongo_try_reconnect(conn);
+            if(ret != MONGO_OK){
+                return MONGO_ERROR;
+            }
+            goto RETRY;
+        }
+    }
     return MONGO_OK;
 }
 
@@ -1223,16 +1272,16 @@ static int mongo_cursor_get_more( mongo_cursor *cursor ) {
         data = mongo_data_append32( data, &limit );
         mongo_data_append64( data, &cursor->reply->fields.cursorID );
 
-        bson_free( cursor->reply );
+        //bson_free( cursor->reply );
         res = mongo_message_send( cursor->conn, mm );
         if( res != MONGO_OK ) {
-            mongo_cursor_destroy( cursor );
+            //mongo_cursor_destroy( cursor );
             return MONGO_ERROR;
         }
 
         res = mongo_read_response( cursor->conn, &( cursor->reply ) );
         if( res != MONGO_OK ) {
-            mongo_cursor_destroy( cursor );
+            //mongo_cursor_destroy( cursor );
             return MONGO_ERROR;
         }
         cursor->current.data = NULL;
@@ -1330,14 +1379,15 @@ MONGO_EXPORT int mongo_cursor_next( mongo_cursor *cursor ) {
 
     if( ! ( cursor->flags & MONGO_CURSOR_QUERY_SENT ) )
         if( mongo_cursor_op_query( cursor ) != MONGO_OK )
+        {
             return MONGO_ERROR;
-
+        }
     if( !cursor->reply )
+    {
         return MONGO_ERROR;
-
+    }
     /* no data */
     if ( cursor->reply->fields.num == 0 ) {
-
         /* Special case for tailable cursors. */
         if( cursor->reply->fields.cursorID ) {
             if( ( mongo_cursor_get_more( cursor ) != MONGO_OK ) ||
@@ -1347,7 +1397,9 @@ MONGO_EXPORT int mongo_cursor_next( mongo_cursor *cursor ) {
         }
 
         else
+        {
             return MONGO_ERROR;
+        }
     }
 
     /* first */
@@ -1361,8 +1413,9 @@ MONGO_EXPORT int mongo_cursor_next( mongo_cursor *cursor ) {
 
     if ( next_object >= message_end ) {
         if( mongo_cursor_get_more( cursor ) != MONGO_OK )
+        {
             return MONGO_ERROR;
-
+        }
         if ( cursor->reply->fields.num == 0 ) {
             /* Special case for tailable cursors. */
             if ( cursor->reply->fields.cursorID ) {
@@ -1370,7 +1423,9 @@ MONGO_EXPORT int mongo_cursor_next( mongo_cursor *cursor ) {
                 return MONGO_ERROR;
             }
             else
+            {
                 return MONGO_ERROR;
+            }
         }
 
         bson_init_finished_data( &cursor->current, &cursor->reply->objs );
