diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py
index 60c4c0005cc0deb47ba631c81f2fcab329c3d543..5677abe6b5ef4610bc2298b630f9086aa26f8024 100644
--- a/src/common/connection_handlers.py
+++ b/src/common/connection_handlers.py
@@ -1323,7 +1323,7 @@ sent a message to.'''
 			orphaned = filter(lambda s: not s.control, chat_sessions)
 
 			return orphaned[0]
-		except KeyError:
+		except (KeyError, IndexError):
 			return None
 
 	def make_new_session(self, jid, thread_id=None, type='chat', cls=None):
@@ -2104,6 +2104,13 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
 				self.dispatch('ERROR_ANSWER', ('', jid_stripped,
 					errmsg, errcode))
 
+		if ptype == 'unavailable' and jid_stripped in self.sessions:
+			# automatically terminate sessions that they haven't sent a thread ID in
+			for sess in self.sessions[jid_stripped].values():
+				if not sess.received_thread_id:
+					sess.terminate()
+					del self.sessions[jid_stripped][sess.thread_id]
+
 		if avatar_sha and ptype != 'error':
 			if not self.vcard_shas.has_key(jid_stripped):
 				cached_vcard = self.get_cached_vcard(jid_stripped)
diff --git a/src/gajim.py b/src/gajim.py
index 47d90a8d201da4337586774acf0c236805e70fb2..20867a677b321457407dd936ed64058ba4781013 100755
--- a/src/gajim.py
+++ b/src/gajim.py
@@ -621,7 +621,7 @@ class Interface:
 		jid = array[0].split('/')[0]
 		keyID = array[5]
 		contact_nickname = array[7]
-		
+
 		# Get the proper keyID
 		keyID = helpers.prepare_and_validate_gpg_keyID(account, 
 				jid, keyID)
@@ -647,6 +647,20 @@ class Interface:
 				if c.resource == resource:
 					contact1 = c
 					break
+
+			highest = gajim.contacts.get_highest_prio_contact_from_contacts(lcontact)
+			if not highest or \
+			(highest.priority < priority and highest.resource != resource) or \
+			(highest.resource == resource and highest.priority > priority):
+				# either this contact is the new highest priority contact or it was the
+				# highest and dropped in priority (so may no longer be the highest)
+
+				# disconnect sessions from this contact's chat controls so we
+				# don't have to open a new tab if a new session comes in
+
+				for ctrl in self.msg_win_mgr.get_chat_controls(jid, account):
+					ctrl.set_session(None)
+
 			if contact1:
 				if contact1.show in statuss:
 					old_show = statuss.index(contact1.show)
diff --git a/src/session.py b/src/session.py
index d2823c89b7860e9aecdcba846015eb99e27ef06e..3c1ef65bcfaaa56cfc803586e2be39768e2790ad 100644
--- a/src/session.py
+++ b/src/session.py
@@ -125,15 +125,12 @@ class ChatControlSession(stanza_session.EncryptedStanzaSession):
 			pm = True
 			msg_type = 'pm'
 
-		jid_of_control = full_jid_with_resource
-
 		highest_contact = gajim.contacts.get_contact_with_highest_priority(
 			self.conn.name, jid)
 
-		if not pm:
-			if not highest_contact or not highest_contact.resource or \
-			resource == highest_contact.resource or highest_contact.show == 'offline':
-				jid_of_control = jid
+		# does this resource have the highest priority of any available?
+		is_highest = not highest_contact or not highest_contact.resource or \
+			resource == highest_contact.resource or highest_contact.show == 'offline'
 
 		# Handle chat states
 		contact = gajim.contacts.get_contact(self.conn.name, jid, resource)
@@ -174,11 +171,10 @@ class ChatControlSession(stanza_session.EncryptedStanzaSession):
 		advanced_notif_num = notify.get_advanced_notification('message_received',
 			self.conn.name, contact)
 
-		# Is it a first or next message received ?
-		first = False
-		if not self.control and not gajim.events.get_events(self.conn.name,
-																							jid_of_control, [msg_type]):
-			first = True
+		if not pm and is_highest:
+			jid_of_control = jid
+		else:
+			jid_of_control = full_jid_with_resource
 
 		if not self.control:
 			# look for an existing chat control without a session
@@ -186,7 +182,6 @@ class ChatControlSession(stanza_session.EncryptedStanzaSession):
 			if ctrl:
 				self.control = ctrl
 				self.control.set_session(self)
-				first = False
 
 		if pm:
 			nickname = resource
@@ -198,17 +193,24 @@ class ChatControlSession(stanza_session.EncryptedStanzaSession):
 				xhtml=xhtml, form_node=form_node)
 
 			nickname = gajim.get_name_from_jid(self.conn.name, jid)
+
 		# Check and do wanted notifications
 		msg = msgtxt
 		if subject:
 			msg = _('Subject: %s') % subject + '\n' + msg
 		focused = False
 
+		# Is it a first or next message received ?
+		first = False
+
 		if self.control:
 			parent_win = self.control.parent_win
 			if self.control == parent_win.get_active_control() and \
 			parent_win.window.has_focus:
 				focused = True
+		elif not gajim.events.get_events(self.conn.name, \
+		jid_of_control, [msg_type]):
+			first = True
 
 		notify.notify('new_message', jid_of_control, self.conn.name, [msg_type,
 			first, nickname, msg, focused], advanced_notif_num)