public function updateWriteStats($written)
    {
        if (!is_int($written)) {
            throw new \InvalidArgumentException('Not an integer');
        }
        $time = time();
        $this->context['rekeyingBytes'] += $written;

        if (isset($this->context['rekeying'])) {
            // Do not restart key exchange
            // if already rekeying.
            return;
        }

        $logging = \Plop\Plop::getInstance();
        $stats = array(
            'bytes' => $this->context['rekeyingBytes'],
            'duration' =>
                $time - $this->context['rekeyingTime'] +
                $this->rekeyingTime,
        );
        $logging->debug(
            '%(bytes)d bytes sent in %(duration)d seconds',
            $stats
        );


        if ($this->context['rekeyingBytes'] >= $this->rekeyingBytes ||
            $time >= $this->context['rekeyingTime']) {
            $logging->debug('Initiating rekeying');
            $this->context['rekeying']      = 'server';
            $this->context['rekeyingBytes'] = 0;
            $this->context['rekeyingTime']  = $time + $this->rekeyingTime;
            $kexinit = new \fpoirotte\Pssht\Handlers\InitialState();
            $kexinit->handleKEXINIT($this, $this->context);
        }
    }