Index: channels/chan_skinny.c
===================================================================
--- channels/chan_skinny.c	(revision 283495)
+++ channels/chan_skinny.c	(working copy)
@@ -1401,6 +1401,7 @@
 static int skinny_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
 static void mwi_event_cb(const struct ast_event *event, void *userdata);
 static int skinny_reload(void);
+static void setsubstate_ringout(struct skinny_subchannel *sub, char exten[AST_MAX_EXTENSION]);
 
 static const struct ast_channel_tech skinny_tech = {
 	.type = "Skinny",
@@ -3931,10 +3932,7 @@
 					}
 					return NULL;
 				} else {
-					ast_copy_string(c->exten, d->exten, sizeof(c->exten));
-					ast_copy_string(l->lastnumberdialed, d->exten, sizeof(l->lastnumberdialed));
-					memset(d->exten, 0, sizeof(d->exten));
-					skinny_newcall(c);
+					setsubstate_ringout(c->tech_pvt, d->exten);
 					return NULL;
 				}
 			} else {
@@ -4721,6 +4719,40 @@
 	}
 }
 
+static void setsubstate_ringout(struct skinny_subchannel *sub, char exten[AST_MAX_EXTENSION])
+{
+	struct skinny_line *l = sub->parent;
+	struct skinny_device *d = l->device;
+	struct ast_channel *c = sub->owner;
+	pthread_t t;
+	
+	if (ast_strlen_zero(exten) || !ast_exists_extension(c, c->context, exten, 1, l->cid_num)) {
+		ast_log(LOG_WARNING, "Exten (%s) does not exist, unable to set substate DIALING on sub %d\n", exten, sub->callid);
+		return;
+	}
+
+	if (l->hookstate == SKINNY_ONHOOK) {
+		l->hookstate = SKINNY_OFFHOOK;
+		transmit_speaker_mode(d, SKINNY_SPEAKERON);
+		transmit_callstate(d, l->instance, sub->callid, SKINNY_OFFHOOK);
+		transmit_activatecallplane(d, l);
+	}
+	transmit_stop_tone(d, l->instance, sub->callid);
+	transmit_clear_display_message(d, l->instance, sub->callid);
+	transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGOUT);
+
+	ast_copy_string(c->exten, exten, sizeof(c->exten));
+	ast_copy_string(l->lastnumberdialed, exten, sizeof(l->lastnumberdialed));
+	memset(d->exten, 0, sizeof(d->exten));
+
+	sub->substate = SUBSTATE_RINGOUT;
+	
+	if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
+		ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
+		ast_hangup(c);
+	}
+}
+
 static void setsubstate_connected(struct skinny_subchannel *sub)
 {
 	struct skinny_line *l = sub->parent;
@@ -4983,7 +5015,6 @@
 	struct skinny_subchannel *sub;
 	/*struct skinny_speeddial *sd;*/
 	struct ast_channel *c;
-	pthread_t t;
 	int event;
 	int instance;
 	int callreference;
@@ -5025,23 +5056,7 @@
 			sub = c->tech_pvt;
 			l = sub->parent;
 			l->activesub = sub;
-			if (l->hookstate == SKINNY_ONHOOK) {
-				l->hookstate = SKINNY_OFFHOOK;
-				transmit_callstate(d, l->instance, sub->callid, SKINNY_OFFHOOK);
-				transmit_activatecallplane(d, l);
-			}
-			transmit_clear_display_message(d, l->instance, sub->callid);
-			transmit_start_tone(d, SKINNY_DIALTONE, l->instance, sub->callid);
-			transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGOUT);
-
-			if (!ast_ignore_pattern(c->context, l->lastnumberdialed)) {
-				transmit_stop_tone(d, l->instance, sub->callid);
-			}
-			ast_copy_string(c->exten, l->lastnumberdialed, sizeof(c->exten));
-			if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
-				ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
-				ast_hangup(c);
-			}
+			setsubstate_ringout(sub, l->lastnumberdialed);
 		}
 		break;
 	case STIMULUS_SPEEDDIAL:
@@ -5065,29 +5080,8 @@
 			sub = c->tech_pvt;
 			l = sub->parent;
 			l->activesub = sub;
-			if (l->hookstate == SKINNY_ONHOOK) {
-				l->hookstate = SKINNY_OFFHOOK;
-				transmit_speaker_mode(d, SKINNY_SPEAKERON);
-				transmit_callstate(d, l->instance, sub->callid, SKINNY_OFFHOOK);
-				transmit_activatecallplane(d, l);
-			}
-			transmit_clear_display_message(d, l->instance, sub->callid);
-			transmit_start_tone(d, SKINNY_DIALTONE, l->instance, sub->callid);
-			transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGOUT);
-
-			if (!ast_ignore_pattern(c->context, sd->exten)) {
-				transmit_stop_tone(d, l->instance, sub->callid);
-			}
-			if (ast_exists_extension(c, c->context, sd->exten, 1, l->cid_num)) {
-				ast_copy_string(c->exten, sd->exten, sizeof(c->exten));
-				ast_copy_string(l->lastnumberdialed, sd->exten, sizeof(l->lastnumberdialed));
-
-				if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
-					ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
-					ast_hangup(c);
-				}
-				break;
-			}
+			
+			setsubstate_ringout(sub, sd->exten);
 		}
 	    }
 		break;
@@ -5124,34 +5118,9 @@
 			sub = c->tech_pvt;
 			l = sub->parent;
 			l->activesub = sub;
+			
+			setsubstate_ringout(sub,l->vmexten);
 
-			if (ast_strlen_zero(l->vmexten))  /* Exit the call if no VM pilot */
-				break;
-
-			if (l->hookstate == SKINNY_ONHOOK){
-				l->hookstate = SKINNY_OFFHOOK;
-				transmit_speaker_mode(d, SKINNY_SPEAKERON);
-				transmit_callstate(d, l->instance, sub->callid, SKINNY_OFFHOOK);
-				transmit_activatecallplane(d, l);
-			}
-
-			transmit_clear_display_message(d, l->instance, sub->callid);
-			transmit_start_tone(d, SKINNY_DIALTONE, l->instance, sub->callid);
-			transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGOUT);
-
-			if (!ast_ignore_pattern(c->context, l->vmexten)) {
-				transmit_stop_tone(d, l->instance, sub->callid);
-			}
-
-			if (ast_exists_extension(c, c->context, l->vmexten, 1, l->cid_num)) {
-				ast_copy_string(c->exten, l->vmexten, sizeof(c->exten));
-				ast_copy_string(l->lastnumberdialed, l->vmexten, sizeof(l->lastnumberdialed));
-				if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
-					ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
-					ast_hangup(c);
-				}
-				break;
-			}
 		}
 		break;
 	case STIMULUS_CALLPARK:
@@ -5711,7 +5680,6 @@
 	struct skinny_line *l;
 	struct skinny_subchannel *sub = NULL;
 	struct ast_channel *c;
-	pthread_t t;
 
 	if (skinnydebug)
 		ast_verb(1, "Received Enbloc Call: %s\n", req->data.enbloccallmessage.calledParty);
@@ -5736,19 +5704,7 @@
 
 		sub = c->tech_pvt;
 		l->activesub = sub;
-		transmit_callstate(d, l->instance, sub->callid, SKINNY_OFFHOOK);
-		transmit_activatecallplane(d, l);
-		transmit_clear_display_message(d, l->instance, sub->callid);
-		transmit_start_tone(d, SKINNY_DIALTONE, l->instance, sub->callid);
-
-		if (!ast_ignore_pattern(c->context, req->data.enbloccallmessage.calledParty)) {
-			transmit_stop_tone(d, l->instance, sub->callid);
-		}
-		ast_copy_string(c->exten, req->data.enbloccallmessage.calledParty, sizeof(c->exten));
-		if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
-			ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
-			ast_hangup(c);
-		}
+		setsubstate_ringout(sub, req->data.enbloccallmessage.calledParty);
 	}
 	
 	return 1;
@@ -5761,7 +5717,6 @@
 	struct skinny_line *l;
 	struct skinny_subchannel *sub = NULL;
 	struct ast_channel *c;
-	pthread_t t;
 	int event;
 	int instance;
 	int callreference;
@@ -5814,24 +5769,7 @@
 		} else {
 			sub = c->tech_pvt;
 			l->activesub = sub;
-			if (l->hookstate == SKINNY_ONHOOK) {
-				l->hookstate = SKINNY_OFFHOOK;
-				transmit_speaker_mode(d, SKINNY_SPEAKERON);
-				transmit_callstate(d, l->instance, sub->callid, SKINNY_OFFHOOK);
-				transmit_activatecallplane(d, l);
-			}
-			transmit_clear_display_message(d, l->instance, sub->callid);
-			transmit_start_tone(d, SKINNY_DIALTONE, l->instance, sub->callid);
-			transmit_selectsoftkeys(d, l->instance, sub->callid, KEYDEF_RINGOUT);
-
-			if (!ast_ignore_pattern(c->context, l->lastnumberdialed)) {
-				transmit_stop_tone(d, l->instance, sub->callid);
-			}
-			ast_copy_string(c->exten, l->lastnumberdialed, sizeof(c->exten));
-			if (ast_pthread_create(&t, NULL, skinny_newcall, c)) {
-				ast_log(LOG_WARNING, "Unable to create new call thread: %s\n", strerror(errno));
-				ast_hangup(c);
-			}
+			setsubstate_ringout(sub, l->lastnumberdialed);
 		}
 		break;
 	case SOFTKEY_NEWCALL:  /* Actually the DIAL softkey */
