Index: channels/chan_skinny.c
===================================================================
--- channels/chan_skinny.c	(revision 367664)
+++ channels/chan_skinny.c	(working copy)
@@ -2061,7 +2061,7 @@
 	AST_LIST_TRAVERSE(&devices, d, list){
 		struct ast_sockaddr addr;
 		ast_sockaddr_from_sin(&addr, &s->sin);
-		if (!strcasecmp(req->data.reg.name, d->id)
+		if (!d->session && !strcasecmp(req->data.reg.name, d->id)
 				&& ast_apply_ha(d->ha, &addr)) {
 			s->device = d;
 			d->type = letohl(req->data.reg.type);
@@ -2087,11 +2087,6 @@
 			}
 			AST_LIST_TRAVERSE(&d->lines, l, list) {
 				/* FIXME: All sorts of issues will occur if this line is already connected to a device */
-				if (l->device) {
-					manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: Skinny\r\nPeer: Skinny/%s@%s\r\nPeerStatus: Rejected\r\nCause: LINE_ALREADY_CONNECTED\r\n", l->name, l->device->name); 
-					ast_verb(1, "Line %s already connected to %s. Not connecting to %s.\n", l->name, l->device->name, d->name);
-				} else {
-					l->device = d;
 					ast_format_cap_joint_copy(l->confcap, d->cap, l->cap);
 					l->prefs = l->confprefs;
 					if (!l->prefs.order[0]) {
@@ -2110,7 +2105,6 @@
 						ast_extension_state_add(subline->context, subline->exten, skinny_extensionstate_cb, subline->container);
 					}
 					ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Skinny/%s", l->name);
-				}
 				--instance;
 			}
 			break;
@@ -2128,12 +2122,15 @@
 	struct skinny_device *d;
 	struct skinny_line *l;
 	struct skinny_speeddial *sd;
+	struct skinny_subchannel *sub;
+	struct ast_channel *chan;
 
 	d = s->device;
 
 	if (d) {
 		d->session = NULL;
 		d->registered = 0;
+		d->hookstate = SKINNY_ONHOOK;
 
 		AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
 			if (sd->stateid > -1)
@@ -2141,7 +2138,24 @@
 		}
 		AST_LIST_TRAVERSE(&d->lines, l, list) {
 			if (l->device == d) {
-				l->device = NULL;
+				AST_LIST_TRAVERSE(&l->sub, sub, list) {
+					AST_LIST_REMOVE(&l->sub, sub, list);
+					ast_mutex_lock(&sub->lock);
+					if (sub->owner) {
+						chan = sub->owner;
+						sub->owner = NULL;
+						ast_channel_tech_pvt_set(chan, NULL);
+						ast_softhangup(chan, AST_SOFTHANGUP_APPUNLOAD);
+					}
+					if (sub->rtp) {
+						ast_rtp_instance_destroy(sub->rtp);
+						sub->rtp = NULL;
+					}
+					ast_mutex_unlock(&sub->lock);
+					ast_free(sub);
+					ast_module_unref(ast_module_info->self);
+				}
+				l->activesub = NULL;
 				ast_format_cap_remove_all(l->cap);
 				ast_parse_allow_disallow(&l->prefs, l->cap, "all", 0);
 				l->instance = 0;
@@ -3140,7 +3154,7 @@
 {
 	struct skinny_line *l = userdata;
 	struct skinny_device *d = l->device;
-	if (d) {
+	if (d && d->session) {
 		struct skinnysession *s = d->session;
 		struct skinny_line *l2;
 		int new_msgs = 0;
@@ -3819,7 +3833,7 @@
 		if (!s) {
 			ast_cli(fd, "%-20s %-20s %8d %-20s\n",
 				l->name,
-				(l->device ? l->device->name : "Not connected"),
+				l->device->name,
 				l->instance,
 				l->label);
 			if (verbose) {
@@ -3842,7 +3856,7 @@
 				"Label: %s\r\n",
 				idtext,
 				l->name,
-				(l->device?l->device->name:"None"),
+				l->device->name,
 				l->instance,
 				l->label);
 		}
@@ -4332,7 +4346,7 @@
 	struct ast_var_t *current;
 	int doautoanswer = 0;
 
-	if (!d->registered) {
+	if (!d->session) {
 		ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
 		return -1;
 	}
@@ -4572,7 +4586,7 @@
 
 	if (!l)
 		res = AST_DEVICE_INVALID;
-	else if (!l->device)
+	else if (!l->device || !l->device->session)
 		res = AST_DEVICE_UNAVAILABLE;
 	else if (l->dnd)
 		res = AST_DEVICE_BUSY;
@@ -4796,7 +4810,7 @@
 	struct ast_variable *v = NULL;
 	struct ast_format tmpfmt;
 
-	if (!l->device) {
+	if (!l->device || !l->device->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return NULL;
 	}
@@ -7408,6 +7422,7 @@
 							}
 							lineInstance++;
 							AST_LIST_INSERT_HEAD(&CDEV->lines, l, list);
+							l->device = CDEV;
 						}
  						break;
  					}
@@ -7649,7 +7664,6 @@
 
 			AST_LIST_LOCK(&d->lines);
 			AST_LIST_TRAVERSE(&d->lines, l, list){
-				l->device = d;	
 
 				AST_LIST_LOCK(&temp->lines);
 				AST_LIST_TRAVERSE(&temp->lines, ltemp, list) {
