def serverInit(metadata, addr, port)
      # A peer connected to us
      # Read handshake message
      @logger.warn "Peer connection from #{addr}:#{port}"
      begin
        msg = PeerHandshake.unserializeExceptPeerIdFrom currentIo
      rescue
        @logger.warn "Peer failed handshake: #{$!}"
        close
        return
      end

      torrentData = torrentDataForHandshake(msg, "#{addr}:#{port}")
      # Are we tracking this torrent?
      if !torrentData
        @logger.warn "Peer sent handshake for unknown torrent"
        close
        return 
      end
      trackerclient = torrentData.trackerClient

      # If we already have too many connections, don't allow this connection.
      classifiedPeers = ClassifiedPeers.new torrentData.peers.all
      if classifiedPeers.establishedPeers.length > @targetActivePeerCount
        @logger.warn "Closing connection to peer from #{addr}:#{port} because we already have #{classifiedPeers.establishedPeers.length} active peers which is > the target count of #{@targetActivePeerCount} "
        close
        return 
      end  

      # Send handshake
      outgoing = PeerHandshake.new
      outgoing.peerId = trackerclient.peerId
      outgoing.infoHash = torrentData.infoHash
      outgoing.serializeTo currentIo

      # Send extended handshake if the peer supports extensions
      if (msg.reserved.unpack("C8")[5] & 0x10) != 0
        @logger.warn "Peer supports extensions. Sending extended handshake"
        extended = Extension.createExtendedHandshake torrentData.info
        extended.serializeTo currentIo
      end
 
      # Read incoming handshake's peerid
      msg.peerId = currentIo.read(PeerHandshake::PeerIdLen)

      if msg.peerId == trackerclient.peerId
        @logger.info "We got a connection from ourself. Closing connection."
        close
        return
      end
     
      peer = nil
      peers = torrentData.peers.findById(msg.peerId)
      if peers
        peers.each do |existingPeer|
          if existingPeer.state != :disconnected
            @logger.warn "Peer with id #{msg.peerId} created a new connection when we already have a connection in state #{existingPeer.state}. Closing new connection."
            close
            return
          else
            if existingPeer.trackerPeer.ip == addr && existingPeer.trackerPeer.port == port
              peer = existingPeer
            end
          end
        end
      end

      if ! peer
        peer = Peer.new(TrackerPeer.new(addr, port))
        updatePeerWithHandshakeInfo(torrentData, msg, peer)
        torrentData.peers.add peer
        if ! peers
          @logger.warn "Unknown peer with id #{msg.peerId} connected."
        else
          @logger.warn "Known peer with id #{msg.peerId} connected from new location."
        end
      else
        @logger.warn "Known peer with id #{msg.peerId} connected from known location."
      end

      @logger.info "Peer #{peer} connected to us. "

      peer.state = :established
      peer.amChoked = true
      peer.peerChoked = true
      peer.amInterested = false
      peer.peerInterested = false
      if torrentData.info
        peer.bitfield = Bitfield.new(torrentData.info.pieces.length)
      else
        peer.bitfield = EmptyBitfield.new
        @logger.info "We have no metainfo yet, so setting peer #{peer} to have an EmptyBitfield"
      end

      # Send bitfield
      sendBitfield(currentIo, torrentData.blockState.completePieceBitfield) if torrentData.blockState

      setMetaInfo(peer)
      setReadRateLimit(torrentData.downRateLimit) if torrentData.downRateLimit
      setWriteRateLimit(torrentData.upRateLimit) if torrentData.upRateLimit
    end
def serverInit(metadata, addr, port)
      # A peer connected to us
      # Read handshake message
      @logger.warn "Peer connection from #{addr}:#{port}"
      begin
        msg = PeerHandshake.unserializeExceptPeerIdFrom currentIo
      rescue
        @logger.warn "Peer failed handshake: #{$!}"
        close
        return
      end

      torrentData = torrentDataForHandshake(msg, "#{addr}:#{port}")
      # Are we tracking this torrent?
      if !torrentData
        @logger.warn "Peer sent handshake for unknown torrent"
        close
        return 
      end
      trackerclient = torrentData.trackerClient

      # If we already have too many connections, don't allow this connection.
      classifiedPeers = ClassifiedPeers.new torrentData.peers.all
      if classifiedPeers.establishedPeers.length > @targetActivePeerCount
        @logger.warn "Closing connection to peer from #{addr}:#{port} because we already have #{classifiedPeers.establishedPeers.length} active peers which is > the target count of #{@targetActivePeerCount} "
        close
        return 
      end  

      # Send handshake
      outgoing = PeerHandshake.new
      outgoing.peerId = trackerclient.peerId
      outgoing.infoHash = torrentData.infoHash
      outgoing.serializeTo currentIo

      # Send extended handshake if the peer supports extensions
      if (msg.reserved.unpack("C8")[5] & 0x10) != 0
        @logger.warn "Peer supports extensions. Sending extended handshake"
        extended = Extension.createExtendedHandshake torrentData.info
        extended.serializeTo currentIo
      end
 
      # Read incoming handshake's peerid
      msg.peerId = currentIo.read(PeerHandshake::PeerIdLen)

      if msg.peerId == trackerclient.peerId
        @logger.info "We got a connection from ourself. Closing connection."
        close
        return
      end
     
      peer = nil
      peers = torrentData.peers.findById(msg.peerId)
      if peers
        peers.each do |existingPeer|
          if existingPeer.state != :disconnected
            @logger.warn "Peer with id #{msg.peerId} created a new connection when we already have a connection in state #{existingPeer.state}. Closing new connection."
            close
            return
          else
            if existingPeer.trackerPeer.ip == addr && existingPeer.trackerPeer.port == port
              peer = existingPeer
            end
          end
        end
      end

      if ! peer
        peer = Peer.new(TrackerPeer.new(addr, port))
        updatePeerWithHandshakeInfo(torrentData, msg, peer)
        torrentData.peers.add peer
        if ! peers
          @logger.warn "Unknown peer with id #{msg.peerId} connected."
        else
          @logger.warn "Known peer with id #{msg.peerId} connected from new location."
        end
      else
        @logger.warn "Known peer with id #{msg.peerId} connected from known location."
      end

      @logger.info "Peer #{peer} connected to us. "

      peer.state = :established
      peer.amChoked = true
      peer.peerChoked = true
      peer.amInterested = false
      peer.peerInterested = false
      if torrentData.info
        peer.bitfield = Bitfield.new(torrentData.info.pieces.length)
      else
        peer.bitfield = EmptyBitfield.new
        @logger.info "We have no metainfo yet, so setting peer #{peer} to have an EmptyBitfield"
      end

      # Send bitfield
      sendBitfield(currentIo, torrentData.blockState.completePieceBitfield) if torrentData.blockState

      setMetaInfo(peer)
      setReadRateLimit(torrentData.downRateLimit) if torrentData.downRateLimit
      setWriteRateLimit(torrentData.upRateLimit) if torrentData.upRateLimit
    end
REACTOR METHODS 
 Reactor method called when a peer has connected to us.