--- gw/smsc/smsc_at.c	2006-05-23 20:27:31.000000000 +0700
+++ gw/smsc/smsc_at.c-	2008-03-24 12:25:47.000000000 +0700
@@ -653,6 +653,8 @@
             }
             if (octstr_search(line, octstr_imm("RING"), 0) != -1) {
                 at2_write_line(privdata, "ATH0");
+                time(&end_time);
+                end_time += timeout;
                 continue;
             }
             if (octstr_search(line, octstr_imm("+CPIN: READY"), 0) != -1) {
@@ -679,6 +681,8 @@
                       octstr_get_cstr(privdata->name), octstr_get_cstr(line));
                 gwlist_append(privdata->pending_incoming_messages, line);
                 line = NULL;
+                time(&end_time);
+                end_time += timeout;
                 continue;
             }
             if (octstr_search(line, octstr_imm("+CMT:"), 0) != -1 ||
@@ -719,6 +723,8 @@
                         O_DESTROY(pdu);
                     }
                 }
+                time(&end_time);
+                end_time += timeout;
                 continue;
             }
             if ((octstr_search(line, octstr_imm("+CMGS:"),0) != -1) && (output)) {
@@ -732,6 +738,9 @@
                           octstr_get_cstr(privdata->name));
                 else
                     *output = temp;
+             	    time(&end_time);
+                    end_time += timeout;
+                    continue;
             }
             /* finally check if we received a generic error */
             if (octstr_search(line, octstr_imm("ERROR"), 0) != -1) {
@@ -1177,12 +1186,7 @@
 
     idle_timeout = 0;
     while (!privdata->shutdown) {
-        l = gw_prioqueue_len(privdata->outgoing_queue);
-        if (l > 0) {
-            at2_send_messages(privdata);
-            idle_timeout = time(NULL);
-        } else
-            at2_wait_modem_command(privdata, 1, 0, NULL);
+        at2_wait_modem_command(privdata, 1, 0, NULL);
 
         while (gwlist_len(privdata->pending_incoming_messages) > 0) {
             at2_read_pending_incoming_messages(privdata);
@@ -1208,6 +1212,11 @@
             }
             memory_poll_timeout = time(NULL);
         }
+        l = gw_prioqueue_len(privdata->outgoing_queue);
+        if (l > 0) {
+            at2_send_messages(privdata);
+            idle_timeout = time(NULL);
+        }
     }
     at2_close_device(privdata);
     mutex_lock(conn->flow_mutex);
@@ -1974,14 +1983,12 @@
 {
     Msg *msg;
 
-    do {
         if (privdata->modem->enable_mms && 
             gw_prioqueue_len(privdata->outgoing_queue) > 1)
             at2_send_modem_command(privdata, "AT+CMMS=2", 0, 0);
 
         if ((msg = gw_prioqueue_remove(privdata->outgoing_queue)))
             at2_send_one_message(privdata, msg);
-    } while (msg);
 }
 
 
@@ -1990,7 +1997,6 @@
     unsigned char command[500];
     int ret = -1;
     char sc[3];
-    int retries = RETRY_SEND;
 
     if (octstr_len(privdata->my_number)) {
         octstr_destroy(msg->sms.sender);
@@ -2010,16 +2016,13 @@
 
     if (msg_type(msg) == sms) {
         Octstr *pdu;
+        int msg_id = -1;
 
         if ((pdu = at2_pdu_encode(msg, privdata)) == NULL) {
             error(2, "AT2[%s]: Error encoding PDU!",octstr_get_cstr(privdata->name));
             return;
         }	
 
-        ret = -99;
-        retries = RETRY_SEND;
-        while ((ret != 0) && (retries-- > 0)) {
-            int msg_id = -1;
             /* 
              * send the initial command and then wait for > 
              */
@@ -2029,13 +2032,15 @@
             debug("bb.smsc.at2", 0, "AT2[%s]: send command status: %d",
                   octstr_get_cstr(privdata->name), ret);
 
-            if (ret != 1) /* > only! */
-                continue;
-                
+            if (ret == 1) {/* > only! */
+ 
             /* 
              * Ok the > has been see now so we can send the PDU now and a 
              * control Z but no CR or LF 
              * 
+             * ret is currently 1, but on successful sending of the PDU
+             * it will be 0
+             *
              * We will handle the 'nokiaphone' types a bit differently, since
              * they have a generic error in accepting PDUs that are "too big".
              * Which means, PDU that are longer then 18 bytes get truncated by
@@ -2081,8 +2086,11 @@
             debug("bb.smsc.at2", 0, "AT2[%s]: send command status: %d",
                   octstr_get_cstr(privdata->name), ret);
 
-            if (ret != 0) /* OK only */
-                continue;
+        }
+        if (ret != 0) {
+            bb_smscconn_send_failed(privdata->conn, msg,
+                    SMSCCONN_FAILED_TEMPORARILY, octstr_create("Requeueing SMS to be sent"));
+        }else{
 
             /* store DLR message if needed for SMSC generated delivery reports */
             if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask)) {
@@ -2101,16 +2109,6 @@
             bb_smscconn_sent(privdata->conn, msg, NULL);
         }
 
-        if (ret != 0) {
-            /*
-             * no need to do counter_increase(privdata->conn->failed) here,
-             * since bb_smscconn_send_failed() will inc the counter on
-             * SMSCCONN_FAILED_MALFORMED
-             */
-            bb_smscconn_send_failed(privdata->conn, msg,
-	        SMSCCONN_FAILED_MALFORMED, octstr_create("MALFORMED"));
-        }
-
         O_DESTROY(pdu);
     }
 }
