 			if closestChild?
 				@emitTo(token.nodeId, 'token.candidate', closestChild.id, distance)
 
-
+		# Is called when we receive a candidate for our token. We will store this
+		# information in the token.
+		#
+		# @param id [String] the string identifier of the candidate
+		# @param distance [Float] the distance to our token's target position
+		#
 		_onTokenCandidate: ( id, distance ) =>
 			unless @token?
 				return

			if oldToken = _(@_tokens).find( ( t ) -> token.id is t.id)
				@_tokens.remove(oldToken)


		_distributeToken: () =>
			token = new Token()
			children = @getChildren()
			randomChild = children[_.random(0,children.length-1)]
			randomChild.emit('token.receive', token.serialize())
			console.log  randomChild.id +  ' received a token with id ', token.id

		_onTokenReceived: (tokenString) =>
			if @token?
				return


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

		# Is called when a peer switches supernode status. We will connect
		# to all new supernodes when we're supernode.
		#
		# @param id [String] the string id the of the peer
		# @param superNode [Boolean] the new supernode status of the 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

		# Attempts to enter the network by requesting a list of supernodes
		# and selecting and connecting to a parent from the list.
		#
		_enterNetwork: ( ) =>

			if @isSuperNode or @_parent?
				return

			# Recursive submethod attempt connections to a list
			# of nodes untill success.
			connectParent =  ( superNodes ) =>
				# If no available supernodes, we promote ourself.
				if superNodes.length is 0
					@setSuperNode(true)
					return

				# We pick a random supernode from the list.
				i = _.random(0, superNodes.length - 1)
				superNode = superNodes[i]
				superNodes.splice(i, 1)

				# If we're connected, just set as parent.
				if peer = @getPeer(superNode.id)
					@setParent(peer, ( accepted ) =>
						unless accepted
							connectParent(superNodes)
					)
				
				# Else, first connect, then set as parent.
				else
					peer = @connect(superNode.id)
					peer.once('channel.opened', ( ) =>
						@setParent(peer, ( accepted ) =>
							unless accepted
								connectParent(superNodes)
						)
					)
			# Query the server for nodes, and pass attempt to connect to supers.
			@server.query('nodes', 'node.structured', ( nodes ) =>
				superNodes = _(nodes).filter( ( node ) -> node.isSuperNode)
				connectParent(superNodes)
			)

		# Selects the supernode with the lowest latency and attempts to connect.
		# If this fails, try the next lowest latency.
		#
		_selectParent: ( ) ->

			if @isSuperNode or @_parent?
				return

			# Recursive submethod attempt connections to a list
			# of nodes untill success.
			requestParent = (superNodes) =>
				# If no supernode is left, request from the server.
				if superNodes.length is 0
					@_enterNetwork()
					return

				# Connect to the lowest latency node.
				superNode = superNodes.shift()
				@setParent(superNode, (accepted) =>
					unless accepted
						requestParent(superNodes)
				)

			# Compile a list of connected supernodes and sort by latency.
			superNodes = _(@getPeers()).filter( (peer) -> peer.isSuperNode)
			superNodes = _(superNodes).sortBy( (peer) -> peer.latency)
			requestParent(superNodes)

		# Ensures that the foundation of this node remains valid. Will do  this
		# by connecting to supernodes when required, or setting a parent when we
		# don't have one.
		#
		_updateFoundationNodes: () =>
			# Make sure we have a parent when we need one.
			if not @isSuperNode and not @_parent?
				@_selectParent()
				return

			# Request all nodes from the server and set all supernodes as siblings.
			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)
				)

			# Ensure we are connected to enough supernodes to aid us in finding
			# our correct position in the network and to catch our fall we we 
			# lose our parent.
			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)
