Stop the blockstackd server.

def stop_server( working_dir, clean=False, kill=False ):
    """
    Stop the blockstackd server.
    """

    timeout = 1.0
    dead = False

    for i in xrange(0, 5):
        # try to kill the main supervisor
        pid_file = get_pidfile_path(working_dir)
        if not os.path.exists(pid_file):
            dead = True
            break

        pid = read_pid_file(pid_file)
        if pid is not None:
            try:
               os.kill(pid, signal.SIGTERM)
            except OSError, oe:
               if oe.errno == errno.ESRCH:
                  # already dead
                  log.info("Process %s is not running" % pid)
                  try:
                      os.unlink(pid_file)
                  except:
                      pass

                  return

            except Exception, e:
                log.exception(e)
                os.abort()

        else:
            log.info("Corrupt PID file.  Please make sure all instances of this program have stopped and remove {}".format(pid_file))
            os.abort()

        # is it actually dead?
        blockstack_opts = get_blockstack_opts()
        srv = BlockstackRPCClient('localhost', blockstack_opts['rpc_port'], timeout=5, protocol='http')
        try:
            res = blockstack_ping(proxy=srv)
        except socket.error as se:
            # dead?
            if se.errno == errno.ECONNREFUSED:
                # couldn't connect, so infer dead
                try:
                    os.kill(pid, 0)
                    log.info("Server %s is not dead yet..." % pid)

                except OSError, oe:
                    log.info("Server %s is dead to us" % pid)
                    dead = True
                    break
            else:
                continue

        log.info("Server %s is still running; trying again in %s seconds" % (pid, timeout))
        time.sleep(timeout)
        timeout *= 2

    if not dead and kill:
        # be sure to clean up the pidfile
        log.info("Killing server %s" % pid)
        clean = True
        try:
            os.kill(pid, signal.SIGKILL)
        except Exception, e:
            pass

    if clean:
        # blow away the pid file
        try:
            os.unlink(pid_file)
        except:
            pass


    log.debug("Blockstack server stopped")