 
 			@token.candidates.push(candidate)
 
+		# Selects the best candidate from all token candidates and passes our 
+		# token to this candidate.
+		#
 		_selectTokenOwner: ( ) =>
 			unless @token?
 				return

			else if peer.role is Peer.Role.Child
				@removeChild(peer)


		_onPeerSetSuperNode: (id, superNode) =>
			unless peer = @getPeer(id)
				if superNode and @isSuperNode
					peer = @connect(id)
					peer.once('channel.opened', =>
						@addSibling(peer)
					)
			else
				peer.isSuperNode = superNode

		_enterNetwork: () =>

			if @isSuperNode or @_parent?
				return

			connectParent =  (superNodes) =>

				if superNodes.length is 0
					@setSuperNode(true)
					return

				i = _.random(0,superNodes.length-1)
				superNode = superNodes[i]
				superNodes.splice(i, 1)

				if peer = @getPeer(superNode.id)
					@setParent(peer, (accepted) =>
						unless accepted
							connectParent(superNodes)
					)
				else
					peer = @connect(superNode.id)
					peer.once('channel.opened', () =>
						@setParent(peer, (accepted) =>
							unless accepted
								connectParent(superNodes)
						)
					)

			@server.query('nodes', 'node.structured', (nodes) =>
				superNodes = _(nodes).filter( (node) -> node.isSuperNode)

				if superNodes.length is 0
					@setSuperNode(true)
					return

				connectParent(superNodes)

			)

		_selectParent: () ->

			if @isSuperNode or @_parent?
				return

			requestParent = (superNodes) =>

				if superNodes.length is 0
					@_enterNetwork()
					return

				superNode = superNodes.shift()

				@setParent(superNode, (accepted) =>
					unless accepted
						requestParent(superNodes)
				)

			superNodes = _(@getPeers()).filter( (peer) -> peer.isSuperNode)
			superNodes = _(superNodes).sortBy( (peer) -> peer.latency)
			requestParent(superNodes)

		_updateFoundationNodes: () =>


			if @isSuperNode
				@server.query('nodes', 'node.structured', (nodes) =>
					superNodes = _(nodes).filter( (node) -> node.isSuperNode)

					if superNodes.length is 0
						return

					for superNode in superNodes
						( (superNode) =>

							if peer = @getPeer(superNode.id)
								@addSibling(peer)
							else
								peer = @connect(superNode.id)
								peer.once('channel.opened', () =>
									@addSibling(peer)
								)
						) (superNode)
				)

			else unless @_parent?
				@_selectParent()

			else
				current = _(@getPeers()).filter( (peer) -> peer.isSuperNode).length
				needed = @_foundationNodes - current
				if needed <= 0 
					return

				@_parent.query('siblings', (superNodes) =>
					if superNodes.length is 0
						return 

					superNodes = _(superNodes).filter( (node) => not @getPeer(node.id)?)
					n = Math.min(needed, superNodes.length)
					while superNodes.length > n
						i = _.random(0,superNodes.length-1)

					)
				) ( peer )

		# Computes our position in the network from the positions of our neighbours
		# and the latency to them. This implements the vivaldi network coordinates:
		# http://en.wikipedia.org/wiki/Vivaldi_coordinates
		#
		_computePosition: ( ) ->
			for peer in @getPeers()
				direction = peer.position.subtract(@position)				# Vector to peer
				distance = peer.position.getDistance(@position)				# Distance between node and peer
