Index: channels/chan_skinny.c
===================================================================
--- channels/chan_skinny.c	(revision 369039)
+++ channels/chan_skinny.c	(working copy)
@@ -1880,7 +1880,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);
@@ -1905,12 +1905,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;
 					l->capability = l->confcapability & d->capability;
 					l->prefs = l->confprefs;
 					if (!l->prefs.order[0]) {
@@ -1926,7 +1920,6 @@
 					/* initialize MWI on line and device */
 					mwi_event_cb(0, l);
 					ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Skinny/%s@%s", l->name, d->name);
-				}
 				--instance;
 			}
 			break;
@@ -1957,7 +1950,6 @@
 		}
 		AST_LIST_TRAVERSE(&d->lines, l, list) {
 			if (l->device == d) {
-				l->device = NULL;
 				l->capability = 0;
 				ast_parse_allow_disallow(&l->prefs, &l->capability, "all", 0);			
 				l->instance = 0;
@@ -2677,7 +2669,7 @@
 	struct skinny_line *l = sub->parent;
 	struct skinny_device *d = l->device;
 
-	if (!d) {
+	if (!d->session) {
 		return;
 	}
 
@@ -2731,7 +2723,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;
@@ -3814,7 +3806,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;
 	}
@@ -3922,7 +3914,7 @@
 	struct skinny_line *l = sub->parent;
 	struct skinny_device *d = l->device;
 
-	if (!d || !d->registered) {
+	if (!d || !d->session) {
 		ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
 		return -1;
 	}
@@ -3986,7 +3978,7 @@
 	l = sub->parent;
 	d = l->device;
 
-	if (!d) {
+	if (!d || !d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return 0;
 	}
@@ -4238,7 +4230,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;
@@ -4390,7 +4382,7 @@
 	struct skinny_device *d = l->device;
 	struct skinnysession *s;
 
-	if (!d) {
+	if (!d || !d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return -1;
 	}
@@ -4514,7 +4506,7 @@
 	struct ast_variable *v = NULL;
 	int fmt;
 
-	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;
 	}
@@ -4631,7 +4623,7 @@
 	if (!sub || !sub->owner) {
 		return 0;
 	}
-	if (!d) {
+	if (!d || !d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return 0;
 	}
@@ -4663,7 +4655,7 @@
 	if (!sub || !sub->owner) {
 		return 0;
 	}
-	if (!d) {
+	if (!d || !d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return 0;
 	}
@@ -4720,7 +4712,7 @@
 	l = sub->parent;
 	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;
 	}
@@ -4856,7 +4848,7 @@
 	struct ast_channel *c = sub->owner;
 	pthread_t t;
 
-	if (!d) {
+	if (!d || !d->session) {
 		ast_log(LOG_WARNING, "Device for line %s is not registered.\n", l->name);
 		return 0;
 	}
@@ -7042,6 +7034,7 @@
 							}
 							lineInstance++;
 							AST_LIST_INSERT_HEAD(&CDEV->lines, l, list);
+							l->device = d;
 						}
  						break;
  					}
@@ -7213,7 +7206,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) {
