Index: channels/chan_skinny.c
===================================================================
--- channels/chan_skinny.c	(revision 369039)
+++ 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);
@@ -2086,12 +2086,6 @@
 				instance++;
 			}
 			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 +2104,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;
@@ -2141,7 +2134,6 @@
 		}
 		AST_LIST_TRAVERSE(&d->lines, l, list) {
 			if (l->device == d) {
-				l->device = NULL;
 				ast_format_cap_remove_all(l->cap);
 				ast_parse_allow_disallow(&l->prefs, l->cap, "all", 0);
 				l->instance = 0;
@@ -3104,7 +3096,7 @@
 	struct skinny_line *l = sub->line;
 	struct skinny_device *d = l->device;
 
-	if (!d) {
+	if (!d->session) {
 		return;
 	}
 
@@ -3144,7 +3136,7 @@
 {
 	struct skinny_line *l = userdata;
 	struct skinny_device *d = l->device;
-	if (d) {
+	if (d->session) {
 		struct skinnysession *s = d->session;
 		struct skinny_line *l2;
 		int new_msgs = 0;
@@ -4228,7 +4220,7 @@
 	int res = 0;
 	int loop_pause = 100;
 
-	if (!d) {
+	if (!d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return NULL;
 	}
@@ -4341,7 +4333,7 @@
 	struct ast_var_t *current;
 	int doautoanswer = 0;
 
-	if (!d || !d->registered) {
+	if (!d || !d->session) {
 		ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
 		return -1;
 	}
@@ -4581,7 +4573,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;
@@ -4811,7 +4803,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;
 	}
@@ -4976,7 +4968,7 @@
 	pthread_t t;
 	int actualstate = state;
 
-	if (!l->device) {
+	if (!d || !d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return;
 	}
@@ -5370,7 +5362,7 @@
 	struct skinny_subchannel *activatesub = NULL;
 	struct skinny_subchannel *tsub;
 
-	if (!l->device) {
+	if (!d || !d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return;
 	}
@@ -5487,7 +5479,7 @@
 	l = sub->line;
 	d = l->device;
 
-	if (!d) {
+	if (!d || !d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return -1;
 	}
@@ -5536,7 +5528,7 @@
 	struct skinny_device *d = l->device;
 	struct ast_channel *c = sub->owner;
 
-	if (!d) {
+	if (!d || !d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return 0;
 	}
@@ -7443,6 +7435,7 @@
 							}
 							lineInstance++;
 							AST_LIST_INSERT_HEAD(&CDEV->lines, l, list);
+							l->device = d;
 						}
  						break;
  					}
@@ -7684,7 +7677,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) {
